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.

Mahi
  • 9
  • 2
  • Are looking for this: [Convert Swift string to array](https://stackoverflow.com/questions/25921204/convert-swift-string-to-array) ? – Martin R Aug 13 '19 at 09: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) – Mahi Aug 13 '19 at 09: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. – Martin R Aug 13 '19 at 09: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. – Mahi Aug 13 '19 at 09:57
  • For Swift 3 it would be `Array(s.characters)` – also covered in the above-mentioned Q&A. – Martin R Aug 13 '19 at 09:59
  • You could use http://online.swiftplayground.run/ to use Swift 5. For `isVowel`, it should take uppercased vowels into account. Both `toCharacter()` and `removeVowels(from:)` are wrong – ielyamani Aug 13 '19 at 10:39
  • I have now closed this as a duplicate because your immediate question “How to convert String to Character Array?” is answered in that Q&A, for all Swift versions. – But note that there are several more problems in your code. In addition to what @ielyamani said, all your loops over array indices go beyond the end of the array. But that is better asked in a new question, if necessary. – Martin R Aug 13 '19 at 11:02
  • Turns out the very first thing I was doing was right. Yes, I just checked the loops. They're going out of bound. My bad. But thing is right now, Swift 5 removed the ".characters" property in String that converted it directly to an Array of type specified. And TeamTreeHouse is using a swift version older than 3 that doesn't has String type conforming to Sequence protocol. Hence, both the codes were incompatible on other platform. Anyways, Thanks a lot!! – Mahi Aug 13 '19 at 11:56

1 Answers1

1

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
Eternal Black
  • 259
  • 2
  • 15