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

Requesting Help For Higher Order Functions Part 2 of 3

I don't know how to use Characters to get the first letter from any given string.

Here is the code so far:

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

func firstLetter(s: String) -> String {

    return (s: "Danny")
}

2 Answers

Hi Danny,

Thanks for providing a link to the challenge. below is the code that was accepted there as a solution:

 extension String {
    func modify(f: String -> String) -> String {
        return f(self)
    }
}
func firstLetter(s: String) -> String {

    return "\(s.characters.first!)"
}
let value = "Swift".modify(firstLetter)

Regarding your code, there minor changes needed: 1) return x("") should be return x(self) // you are extending the String class , so the function need to operate on self not a n empty string 2) you need to remove y: from your last line, so first letter takes one parameters, so you dont need to use its name. xcode didnt accept that on my side

I hope this would be helpful. Safwat

Thank you for your response! That did work for me and so did your explanation on how to change the code.

The following code should work, but I'm not sure if it will answer your question. In function firstLetter you should put the actual logic that will get first letter, then pass the fucntion itself as a parameter to the modify function. It will be helpful f you share a link to the video or code challenge so I can see the exact problem. ''' extension String { func modify( x: (String) -> String) -> String {

    return x(self)
}

}

func firstLetter(s: String) -> String {

let firstLetter = s.characters.first!
return ("\(firstLetter)")

}

let x = "Michael" x.modify(firstLetter)

Thanks for responding Safwat! I worked a little more on this and I'm closer but I'm still running into some issues. When I use this solution, it says: "Bummer! Make sure that your function signature matches the requirement specified in the directions" and the preview says

swift_lint.swift:14:20: error: extraneous argument label 'y:' in call
let y = firstLetter(y: "Danny")

but xCode doesn't have a problem with it.

Here is the code below:

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

func firstLetter(y: String) -> String {
    let firstLetter = y.characters.first!
    return ("\(firstLetter)")
}

let y = firstLetter(y: "Danny")

and a link to the challenge: https://teamtreehouse.com/library/closures-in-swift-2/first-class-functions/higher-order-functions

Thanks