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 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 !