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 can't find a String.CharacterView method that returns first letter as String in the documentation. Any luck anyone?

I tried casting it to a String but it was a no go. Any ideas on this challenge?

functions.swift
// Enter your code below

extension String {

func modify(function: String -> String) -> String {
    return function(self)
}
func firstLetter(word: String) -> String {
    let letters = word.characters
    let letter = String(letters.first)
    return letter
}


}

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Patrick,

Couple little issues with your code:

  1. You need to put that function outside the String extension. We're declaring a function, not a method on String that itself takes a String. Think about how you'd call that, and you'll see why: "Greg".firstLetter(word: "Greg") doesn't make sense. Instead, with a plain old function, we just call firstLetter(word: "Greg")
  2. You can't assume that the first character is just index 0, because strings are a pain in the :peach:. There are entire WWDC videos explaining why, but here's one example: The character é could be either one or two characters, depending on the encoding (here's a bit of podcast talking about this exact thing). So, instead, you should use the string property startIndex.

Here's a solution, not changing much from your code:

solution.swift
// Enter your code below
extension String {

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

}

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

In fact, though, we don't need the characters property at all. We can index directly into the string using startIndex:

trickierSolution.swift
// Enter your code below
extension String {

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

}

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

Cheers :beers:

-Greg

Thanks for your explanation Greg! That helps a ton.

Greg Kaleka I think you should explicitly state this startIndex stuff in the Question, because due to me not knowing it, this question was a pain in the __ :)

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Aditya - I'm just a student like you! Pasan Premaratne might see this and take it into consideration.

For the record, I had to do some research to figure this out myself - Apple documentation, Stack Overflow and Google are your friends :smiley:

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Noted :) For what it's worth, I do want you to have to struggle through some of these things and look through documentation. We can't make content for every line of code in Swift and iOS; reading documentation and finding answers is an important skill.

Part of that is asking good questions in forums which seems to be happening here :)

Haha Greg Kaleka, from your status and the formality of your answer I assumed you were a teacher :)