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?

Sood Lahiri
PLUS
Sood Lahiri
Courses Plus Student 858 Points

Optionals Objective giving error

I am working on the first objective of the optionals quiz, I have turned the returning string to an optional and I am also returning nil if string has not been found. I am doing everything properly but it keeps telling me there is an error. The instructions are:

Using the if-let statement, assign the value from the search function to a constant namedresult. If the name was found, use println to print out the string "Found". When calling thesearch function, pass it the name "Doc".

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

2 Answers

I can't see anything wrong with your code. I have checked back through all other posts on this question and the code is the same there too. Indeed, in my Playground, the code I wrote when I went through this course is still there and is identical to yours!

I wonder if the tests behind the compiler have been amended - one for Amit Bijlani !!

Chris Shaw
Chris Shaw
26,676 Points

Looks like the compiler is having intermittent issues as I got it to trigger a real error then put the code back as it is above and it worked fine, try it by using the below then use the above if let statement.

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

Well spotted - absolutely correct. If you force an error then resubmit correct code, the challenges passes!

How bizarre!

Sood Lahiri
PLUS
Sood Lahiri
Courses Plus Student 858 Points

Thanks for the help, seems like there was a compiler error and you first have to show an error and then it compiles properly.