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 Functions and Optionals Optionals What is an Optional?

Antoni Currie
Antoni Currie
7,914 Points

Swift optionals challenge stage 3

Hey guys, I can't figure out whats wrong in my code here for the task 2 of 2 in Swift Functions ans Optionals...

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 } return "" }

if let result = search("Doc") { println("Found") }

3 Answers

Remember from previous courses that a function that has a parameter prefixed with #, that parameter is also a label which must be specified when calling the function.

if let result = search(name: "Doc"){
    println("Found");
}

First if that's the formatting you are going to use the compiler (or Playground) will complain about "Consecutive statements on a line must be separated by a ';'" - there are 3 instance of that in your code.

And you're missing the label 'name' in the last line :)

If you put all of this in a Playground and click through the errors you'll get helpful hints.

I might also suggest that you rewrite the function in a 'cleaner' manner. Not being critical, just trying to help. You are obviously close to the right track here :)

Antoni Currie
Antoni Currie
7,914 Points

Got it...

Thanks guys. :)

It is written cleaner but when I copy/pasted it it seemed to all come out weird... sorry about that.