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 Swift 2.0 Functions Functions in Swift 2.0 Functions in Code

Julien Schmitt
Julien Schmitt
2,334 Points

Use of unresolved identifier in func. Getting errors in xCode when reproducing what was done in iOS Dev Library.Why?

I read the iOS Dev Library on functions and I tried reproducing some of the functions listed there : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html (the sayHello & sayHelloAgain)

I reproduced the exact same code in xCode however, I'm getting errors of unresolved identifier on my second function. Why ?

func sayHelloAgain(personName: String) -> String { let helloAgain = "Hello again, (personName)!" return helloAgain }

func sayHello(greetingsTo greetingName: String, alreadyGreeted: Bool) -> String { if alreadyGreeted { return sayHelloAgain(personName) <-- Use of unresolved identifier } else { return sayHello(greetingName) <-- Missing argument for parameter 'alreadyGreeting' in call. } }

I have a few questions here:

  1. Why am I getting these errors (identifier & missing argument)?

  2. When returning another function in a function, why can't we just put : return sayHelloAgain(personName: String) ?

Thanks for your help,

Julien

2 Answers

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

Firstly, in the sayHelloAgain function, you need a backward slash before the open parentheses to correctly use string interpolation:

let helloAgain = "Hello again, \(personName)!"

secondly, you need to put greetingName in the call to sayHelloAgain:

// Call sayHelloAgain and input the parameter you're using for the name in the sayHello function
return sayHelloAgain(greetingName)

and thirdly, in the documentation, there's a previous function you need to include:

func sayHello(personName: String) -> String {
    let greeting = "Hello, " + personName + "!"
    return greeting
}

I hope this helps!

Julien Schmitt
Julien Schmitt
2,334 Points

Hi Anjali,

Indeed this helped me :)

Thanks !