Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.