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?

Daniel Ahmed
Daniel Ahmed
2,034 Points

iOS Swift Optionals Stage 3 Help

I can't seem to figure out what the console is requiring. The console responds by saying "found" in the second if-let statement is false. I can't seem to figure out why.

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
}

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

if let searchresult = found {
  println("Found")
}

search(name: "Doc")

2 Answers

Stone Preston
Stone Preston
42,016 Points

the task states: 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".

we need to use what is known as optional binding. see the below excerpt from the Swift eBook:

You use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. Optional binding can be used with if and while statements to check for a value inside an optional, and to extract that value into a constant or variable, as part of a single action. if and while statements are described in more detail in Control Flow.

Write optional bindings for the if statement as follows:

if let constantName = someOptional {
    statements
}

we can use the if let statement to unwrap the optional into a local constant. the code in this block will only get executed if the optional returns a value that is not nil

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") {
   // result has a value, name was found
   println("Found")
}
Daniel Ahmed
Daniel Ahmed
2,034 Points

Thanks. But I have another question. Can we use ("found (result)") ? Instead of just "Found"?

Stone Preston
Stone Preston
42,016 Points

you need to include the backslash when using interpolation:

println("found \(result)")

however the challenge probably wont accept that as an answer since it specifically asks for just "Found"

Daniel Ahmed
Daniel Ahmed
2,034 Points

Thank you helped a lot.