Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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)

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