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?

thomas howard
thomas howard
17,572 Points

What's wrong with Stage 3 Optionals. I'm getting an error with calling "Doc" in the search function. Why?

Why is the code getting an error? The error that I get points at the search("Doc") function. I've watched the video on it like 10 times and still cannot figure this out.

thoughts?

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("Doc") {
  println("Found: \(result)")
}

2 Answers

Michael Hulet
Michael Hulet
47,912 Points

Adding the octothorpe in your function declaration makes it necessary to write the name of the name variable in any calls to the function. See if this works:

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
}
//Notice how you must write the name of the variable called name in the call to search below
if let result = search(name: "Doc") {
  println("Found: \(result)")
}

Alternatively, you could remove the octothorpe in the definition of the search function, and the rest of your original code would work, like this:

//Notice the lack of a # in the below line
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: \(result)")
}
thomas howard
thomas howard
17,572 Points

Number sign - Wikipedia, the free encyclopedia en.wikipedia.org/wiki/Number_sign Wikipedia Octothorp, octothorpe, octathorp, octatherp: Used by Bell Labs engineers by 1968. Lauren Asplund says that he and a colleague were the source of octothorp at ...

I had to google octothorpe!

I see now. That explains the error that I was getting. So remove the octothorpe, or make sure to be specific as to the name of the variable being called by the function.

Also, I did not need to put anything else beside "Found" in the println function.

Thanks, worked.

Tom

Michael Hulet
Michael Hulet
47,912 Points

I realized right after I submitted my answer that that word is rather exotic. I was about to include a link to dictionary.com or something, but my class period ended right before I could do it (I'm still in high school, and today is the last day before Christmas Break). I'm glad it helped!