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?

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?