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 trialRamiro Aguirre
Courses Plus Student 16,725 PointsOptionals issue
The search function below returns a name if it is found in the array and an empty string if it is not found. Modify the search function to return an optional instead of just a String. In addition, you have to make sure the function returns a nil if the name is not found in the array.
this is what i have 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 "" }
if let something = search(name: "Doc") { println("Name Found: (something)") }
and i get
Bummer! You need to define a func
named search
that retruns an optional using the String?
.
1 Answer
Ramiro Aguirre
Courses Plus Student 16,725 Pointsoh never mind i just figured it out
i just added a "?" after ->String? like that and then change the return value tu be nil and then changed the name in the if statment to Snow White and that did it :)
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 something = search(name: "Snow White") { println("Name Found: (something)") }
Nick Honegger
Front End Web Development Techdegree Student 8,773 PointsNick Honegger
Front End Web Development Techdegree Student 8,773 PointsI made the same mistake, is the "?" required for all nil statements?
Srinivasan Senthil
2,266 PointsSrinivasan Senthil
2,266 PointsThanks.