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?

Is this a good solution as well?

func findApt (aptNumber : String) -> String? {

let aptNumbers = ["101","202","303","404"]
for tempAptNumber in aptNumbers {
    if ( tempAptNumber == aptNumber) {
        return aptNumber
    }
}
return nil

}

let aptNumber = findApt("101")

if aptNumber == nil { println("NIL") }else { println(aptNumber!) }


If you look at my conditional statement. Because there is no change to get a NIL value on my ELSE I think I will be fine.


Another way would be to bring the NIL to ELSE.

if aptNumber != nil { println(aptNumber!)

}else { println("NIL") }


So if i understood it correctly. When using the ! to unwrap I just need to make sure that either I do not have a NIL being returned and if I do, I isolate it. is that right?