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?

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. :-)