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 2 First Class Functions Higher Order Functions

I've got an issue with Closures in Swift 2's challenge

1 - Challenge:

Let's use the two functions we created to on a String literal. On the string Swift, call the modify function and use the firstLetter function as an argument. Assign the result to a constant named value.

2 - My code:

extension String {
    func modify(a: String -> String) -> String {
        return "\(a)"
    }
}

func firstLetter(b: String) -> String {
    return "\(b[b.startIndex])"
}

let value = "Swift".modify(firstLetter)

3 - Issue: Make sure you are calling modify on the String 'Swift' and assigning the result to a constant named value

Kerry Toonen
Kerry Toonen
15,830 Points

I think the problem is that in the first challenge you were supposed to call the function on self.

I was actually having trouble passing part two and the error was saying "Make sure you're declaring a function named firstName, that accepts a String and returns a String" which was weird so I tried your code to get it to pass. Anyways, mine passed with this:

extension String {

func modify(function: String -> String) -> String {

return function(self)
}
}

func firstLetter(b: String) -> String {
return "\(b[b.startIndex])"

}

let value = "Swift".modify(firstLetter)

Thanks Kerry that helped out a lot!