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?

Lauren Hibbs
Lauren Hibbs
1,872 Points

Swift 1.2 and Optionals?

From what i've read in the forums i've figured out that the videos use Swift 1.0 and Xcode is currently on Swift 1.2. I don't really understand what the differences is though, with optionals, and what to do in Swift 1.2. The code from the lesson is

func findApt (aptNumber : String) -> String? {
    let aptNumbers = ["101","202","303","404"]
    for tempAptNumber in aptNumbers {
        if ( tempAptNumber == aptNumber) {
            return aptNumber
        }
    }
    return nil
}

In the video

let culprit = findApt("404")

shows "some 404" For me, it shows just 404.

Does this mean that unwrapping is no longer required and that the ! operator is obsolete?

Additionally, when culprit represents a nil

culprit!

does not create any error messages.

How, when, and where are the bang operator, and optional unwrapping used in Swift 1.2, if at all? Thanks - Lauren

1 Answer