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?

Munir Niaz
Munir Niaz
1,831 Points

Help me in this

Using the if-let statement, assign the value from the search function to a constant named result. If the name was found, use println to print out the string "Found". When calling the search function, pass it the name "Doc".

search.swift
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
}
Anthony Babich
Anthony Babich
5,505 Points
func search(#name: String) -> String? {
    let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
    for n in names {
        if (n == name){          
            return name
        }
    }
    return nil
}

if let person = search(name: "Doc") {

    println("\(person) was found")
}

1 Answer

Reed Carson
Reed Carson
8,306 Points

Thats correct, but you havent followed the directions exactly.

it asks you to assign the value to "result", not "person" and it asks you to print "Found", rather than "(person) was found"

By being very specific with the directions, the challenge is teaching you an additional lesson about coding: you have to be exact and specific. "Value" is not the same as "value" to the compiler, for example. If a value named "number" is expected somewhere in a program, and you pass it something close like "num" or "integer", it will crash.