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

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)"

}