iOS Closures in Swift First Class Functions Higher Order Functions

ABHINAV VERMA
ABHINAV VERMA
6,033 Points

Issue in resolving this function

When I'm implementing this function without adding an external parameter everything works in the removeVowels function as expected, but it doesn't with an external parameter as the input isn't being cast as a (String) -> String rather just as a String. how do I resolve this issue

functions.swift
// Enter your code below
extension String{
func transform (_ retString: (String) -> String) -> String {
  return retString(self)
}
}

func removeVowels(from string: String) -> String {
   var string1 = string.lowercased()

    let vowels: [Character] = ["a", "e", "i", "o", "u"]
    let result = String(string1.characters.filter { !vowels.contains($0) })
    return result
}


let result =  "Hello, World!".transform(removeVowels)
Daniel Santos
Daniel Santos
34,388 Points

I tried this with and without external parameter, and both worked. Can you help me reproduced the problem that you have? Can you post exactly the code you are running, if is not the one above?

1 Answer

ABHINAV VERMA
ABHINAV VERMA
6,033 Points

The above is the exact code . I also tried to change the last line

let result = "Hello,World!".transform(removeVowels(from: "Hello, World")) but that was giving a cast error