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?

David Sheridan
David Sheridan
8,579 Points

Having trouble assigning values with my if let statement

I've tried a few different options now and I don't know where to go next

David Sheridan
David Sheridan
8,579 Points
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("Doc"){
    println("Found")
}
David Sheridan
David Sheridan
8,579 Points
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("Doc"){
    println("Found")
}

3 Answers

Hi David,

The only thing I can see that's wrong is that you've ignored the fact there's a # in front of the parameter. You need to use name: when calling the function.

Clicking on Preview will show you the error. Corrected code looks like:

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"){ // use name:
  println("Found")
}

Your code throws this error:

swift_lint.swift:17:24: error: missing argument label 'name:' in call
if let result = search("Doc"){
                       ^
                       name: 

I hope that helps.

Steve.

David Sheridan
David Sheridan
8,579 Points

Hey Steve, thanks for your help, I adjusted my code but im still having trouble.

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

Hi David,

When you use the parameter label, you don't include the hash. You declare the label with the hash in the method signature but use it without:

func search(#name: String) -> String? { // declared with a hash
.
.

// used without
if let result = search(name: "Doc"){

I hope that gets you through.

Steve.

David Sheridan
David Sheridan
8,579 Points

That worked, thanks for the help!

No problem - glad you got sorted. :-)