Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!