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 Returning Functions

Javier Porras
Javier Porras
11,382 Points

stuck on part 2 of 3 of the challenge.

// Enter your code below extension String { func modify(a: String -> String) -> String { return a(self) } }

func firstLetter(userString: String) -> String{ return "(\userString.characters.first)" }

6 Answers

extension String {

    func modify(function: String -> String) -> String  {
        return function(self)
    }

}

func firstLetter (s: String) -> String {
    return String(s.characters.first!)
}

let value = "Swift".modify(firstLetter)
Ivan Kazakov
PLUS
Ivan Kazakov
Courses Plus Student 43,317 Points

Hello Javier,

here my implementation (and it passes the Challenge):

func firstLetter(text: String) -> String {

    if let firstChar = text.characters.first {

        return String(firstChar)

    } else {

        return ""
    }
}

it would be better to return an optional String?, as there is no guarantee that there will be such thing as a first character at all (if an empty string "" is passed in as a parameter). In this case it'd make sense to return nil, but then it wouldn't satisfy the extension parameter signature (String -> String).

This is why I return an empty string ("") back if there is no first character.

Roger Panella
Roger Panella
5,968 Points

I get an error that says "Make sure you're declaring a function named firstName....", but the instructions say to create a function called "firstLetter". Anyone know which one will I need to use to have my answer pass? Just can't tell if that's the issue or something else.

Bryan Benoit
Bryan Benoit
2,985 Points

Here's another way of going about it that also protects against empty strings, that might be a touch easier to understand

extension String {

    func modify(function: String -> String) -> String{
        return function(self)
    }
}

func firstLetter(a: String) -> String {
    if a.isEmpty {
        return "The string is empty"
    } else {
        return String(a.characters.first!)
}
Vasileios Dimitriou
Vasileios Dimitriou
19,721 Points

I did something like this

extension String{
    func modify(function: String -> String) -> String {
        return function(self)
    }
}

func firstLetter(string: String) -> String {
    var result = String()
    result.append(string.characters.first!)

    return result
}
Jakob Hansen
Jakob Hansen
13,746 Points

I used the startIndex property to access the position of the first character of a String like this, and then casted the character to a String type:

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

func firstLetter(word: String) -> String {
    return String(word[word.startIndex])
}