iOS Closures in Swift First Class Functions Higher Order Functions

mahi
mahi
7,137 Points

This just doesn't work. I don't know why.

This doesn't work. This is coming after 2 hours of trying at 3 in the morning. Please HELP!!

functions.swift
// Enter your code below
import Foundation
extension String{

    func transform (_ function: (String) -> String) -> String{
        return function(self)
    }

    func toCharacter() -> [Character]{
        var working = self.components(separatedBy: "")
        var output: [Character] = []

        for i in 0...working.count{
            output.append(Character(working[i]))
        }

        return output
    }
}

extension Character {
    var isVowel: Bool {
        let vowels: [Character] = ["a", "e", "i", "o", "u"]
        return vowels.contains(self)
    }
}

func removeVowels(from given: String) -> String{

    var working = given.toCharacter()

    for i in 0...working.count{
        if working[i].isVowel{
            working.remove(at: i)
        }
    }

    return String (working)

}
mahi
mahi
7,137 Points

Array(stringName) stringName.characters for I in stringName

don't work anymore in Swift 5

1 Answer

Jhoan Arango
MOD
Jhoan Arango
Treehouse Moderator 12,837 Points

Hello,

Sorry for the delayed answer.

So, the challenges are asking to remove the vowels from a string literal, in this case we want to have a list of what we will consider to be vowels. So we can make an array of "Characters" which conform the String literal. We also may use a loop to go over this array and see if our word contains any of the characters in this array, if it does, then we remove it or in our case, we simply don't add it to our new word.

extension String {

  func transform(_ function: (String) -> String ) -> String {
    return function(self)
  }

}

func removeVowels(from: String) -> String {
   // Our new word to be returned without vowels
    var newWord: String = ""

   // List of vowels
    let vowels: [Character] = ["a","e","i","o","u"]

   // Looping and checking if the word contains any vowels
    for character in from.characters {

      // If the list vowels does NOT contain the character, then we append to the new word
        if !vowels.contains(character) {
            newWord.append(character)
        }
    }

   // Return newly created word
    return newWord
}

// Then we use the new methods we just created
let result = "Hello, World!".transform(removeVowels)

I hope this helps you, please let me know.

mahi
mahi
7,137 Points

heya! glad to hear back. So, I worked it out somehow the next day. Turns out what I was doing was working the first time but the problem was with the TreeHouse compiler and Xcode compiler not using the same Swift version. So I want you to check it out in the back end that which version of swift is being used by the TreeHouse compiler. Here's the problem that was making it so hard

string.characters

doesn't work anymore since apple removed the property after in swift 4. So please check it and update the course and questions accordingly.

further

Array(string)

doesn't work in the TreeHouse compiler and gives an error saying "String can't be converted to Array since type String does not conform to the Sequence Protocol" but in reality, String type conforms to Sequence Protocol since Swift 3.0 and hence

Array(string)

works in Xcode So please look into the above mentioned errors since it makes a new learner discouraged after numerous failed attempts

Jhoan Arango
Jhoan Arango
Treehouse Moderator 12,837 Points

Yes, I believe that this course was for Swift 3, and it still uses the compiler for Swift 3. I was going to leave a PS note on my answer explaining this, but I figured it would have confused you further. But glad you were able to see the problem and solved it. After all, that's what coding is all about.

In Swift 4+ is easier because you can omit the "characters" array.

By the way, it should not discourage you to find errors like these.. You will find many many MANY more even after you are a pro. At least with Swift you will find that the language is currently in evolution, and things will change from time to time, which at some point may even break your perfectly working code in one of your production apps. It's just a matter of research, and keeping up with the changes.

mahi
mahi
7,137 Points

hahahaha. Yeah i know that's what's it all about. By new learners, I didn't mean me. hehehe. I have been coding for 4 years now. Like now professionally but yeah, I have been solving some casual problems and trying to grab new things so am pretty used to finding errors and work on them without being discouraged. Anyways, for students after me, please update the courses with comments in the teacher's notes making them aware of the new things. That'll be great.