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 trialJoy Whitehead
1,223 PointsUnresolved identifier?
I've been trying to complete task 2 for optionals, and to me the code looks fine, but it says that "names" is an unresolved identifier? I've tried everything I can think of. Is it just me or is there something messed up?
Code so far:
func search(#name: String) -> String? {
let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
for n in names {
if n == name {
return n
}
}
return nil
}
let search = names("Doc") {
println("Found")
}
2 Answers
Steve Hunter
57,712 PointsHi Joy,
You've not quite handled the returned optional from the search
function correctly.
search
returns either a string or nil
, depending on whether the name is found. That returned value needs assigning to something. The question says:
Using the if-let
statement, assign the value from the search
function to a constant named result
.
So, you need to start with a constant called result and assign the value returned by search
to that. Phew!
if let result = search(name: "Doc") // starts you off
So, that's the if-let
used to separate the two possible outcomes (String or nil) and the outcome is stored in result
.
Then you need to manage the output, which you did just fine:
if let result = search(name: "Doc"){
println("Found")
}
I hope that helps.
Steve.
Joy Whitehead
1,223 PointsThanks, this helped me a lot! Sorry for the trouble.
Steve Hunter
57,712 PointsNo problem! Glad to be of help.
Steve.