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 trialDavid Sheridan
8,579 PointsHaving trouble assigning values with my if let statement
I've tried a few different options now and I don't know where to go next
David Sheridan
8,579 Pointsfunc search(#name: String) -> String? {
let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
for n in names {
if n == name {
return n
}
}
return nil
}
if let result = search("Doc"){
println("Found")
}
3 Answers
Steve Hunter
57,712 PointsHi David,
The only thing I can see that's wrong is that you've ignored the fact there's a # in front of the parameter. You need to use name:
when calling the function.
Clicking on Preview will show you the error. Corrected code looks like:
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
}
if let result = search(name: "Doc"){ // use name:
println("Found")
}
Your code throws this error:
swift_lint.swift:17:24: error: missing argument label 'name:' in call
if let result = search("Doc"){
^
name:
I hope that helps.
Steve.
David Sheridan
8,579 PointsHey Steve, thanks for your help, I adjusted my code but im still having trouble.
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
}
if let result = search(#name: "Doc"){
println("Found")
}
Steve Hunter
57,712 PointsHi David,
When you use the parameter label, you don't include the hash. You declare the label with the hash in the method signature but use it without:
func search(#name: String) -> String? { // declared with a hash
.
.
// used without
if let result = search(name: "Doc"){
I hope that gets you through.
Steve.
David Sheridan
8,579 PointsThat worked, thanks for the help!
Steve Hunter
57,712 PointsNo problem - glad you got sorted. :-)
David Sheridan
8,579 PointsDavid Sheridan
8,579 Points