0

So, I am stuck on this since most of the internal Swift programs don't work anymore and am still learning. I have tried a lot of things but it still doesn't work.

I have tried internal functions. I have tried custom methods. I have tried putting them in extensions (It is the same as custom methods but I wanted to practice extensions.)


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

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
    }
}

func toString(from input: [Character]) -> String{
    var output = ""
    for i in input{
        output += String(i)
    }
    
    return output
}


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 toString(from: working)
  
}

In an extension to the String type, declare a function named transform. The function should accept as an argument another function of type (String) -> String. For this argument, let's omit the external label. The transform function should also return a String.

Create a function named removeVowels that takes a string and returns a string. Name the external argument label for the parameter from. In the body of the method, return a new String literal with the vowels (in the English language) removed from the value passed in as an argument.

8
  • Are looking for this: Convert Swift string to array ? Commented Aug 13, 2019 at 9:25
  • Nope. I tried that aswell. String Type in swift no longer conforms to Sequence protocol and hence can't be converted to Array Type explicitly using Array(String) Commented Aug 13, 2019 at 9:29
  • 3
    That is not correct (unless you are using Swift 3 or earlier). let s = "abc" ; let chars = Array(s) works perfectly in Swift 4 and 5. Commented Aug 13, 2019 at 9:30
  • Turns out, that works in Xcode but doesn't work with TreeHouse editor (that's where I am learning) Maybe they're using an older swift version. Commented Aug 13, 2019 at 9:57
  • For Swift 3 it would be Array(s.characters) – also covered in the above-mentioned Q&A. Commented Aug 13, 2019 at 9:59

1 Answer 1

3

Beside recommending SwifterSwift as they already have that extension implemented as well as many other useful extensions, this is their implementation.

public extension String {
    /// SwifterSwift: Array of characters of a string.
    var charactersArray: [Character] {
        return Array(self)
    }
}

To be used like this:

let myString:String = "myString"
let chars:[Character] = myString.charactersArray
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.