O'Reilly logo

Xcode 7 Essentials - Second Edition by Jayant Varma, Brett Ohland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Optionals

Before we talk about optionals as a concept, let's look at some code in Swift that throws an error but would compile without issue in Objective-C:

var jobTitle:String = "Thought Leader"
jobTitle = nil // Compile error

Note

Objects that are declared as non-optionals can never be set to nil.

If you're an experienced programmer looking at Swift for the first time, this compilation error could come as a pretty big surprise. When you consider this fact, you come to the realization that a whole class of common programming errors simply cannot be made using Swift. For example, if you were to append two strings together in Objective-C and one of them is nil, the app would crash at runtime. This is not possible using Swift and it is exciting news. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required