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?

What did i miss?

i can’t see anything wrong with the code but it keeps telling me in the error that i messed up when calling the function.

search.swift
func search(name: String) -> String? {
    let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
    for aName in names {
        if aName == name {
            return name
        }
    }
    return nil
}

if let person = search("Doc") {
println("I found \(person)")
}

2 Answers

stevedouglas
stevedouglas
7,670 Points

Hopefully this below may help you out as a general outline:

1) The function returns "n" in the array "names" because that is really the value we want. You probably want to return "aName" in your example instead of "name" because you are wanting to return the value in the array you searched for.

2) When I went to look at the instructions, it asked to label store the function in a constant called "result."

3) When I went to look at the instructions, it asked to println("Found").

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

I'm still learning too so I'm sure I have room for improvements in my code as well but the course said this was okay!

thnks