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

Tyler Travis
Tyler Travis
2,429 Points

My code functions properly in Xcode and performs correctly but doesn't pass the test. What is going wrong?

I've tried putting the function block in and outside of the extension, makes no difference. What is going on?

functions.swift
// Enter your code below

extension String {

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



}

func firstLetter(str: String) -> String {

    var character = str.characters.first

    return "\(character)"

  }

1 Answer

Mohamed Moukhtar
PLUS
Mohamed Moukhtar
Courses Plus Student 6,151 Points

Hello Tyler,

The "first" property returns an optional value and you need to unwrap it first in order to pass the challenge.

The working code should be something like the following code:

extension String {

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

}

func firstLetter(str: String) -> String {

let firstCharacter = str.characters.first
guard let character = firstCharacter else {
    return ""
}
return "\(character)"

}