Tuesday, November 17, 2015

Swift Optionals: A Brief Overview (Swift 2.1)

An Optional in Swift is a datatype that's not used by any other language, but serves a great purpose as it guards against null pointer exceptions.  This article explains what Optionals are, and how the can be used.

Primative datatypes (Ints, Strings, Floats, etc) can't store nil values, as is the same in any other object oriented programming language:

But in Swift, even object references can't store nil:

That's where Optionals are useful.  If you have a variable that could have a nil value, then it should be declared as an Optional.  You're giving it the option of either having a value, or having no value.

Declaring Optionals

For example, in some cases a method can't return a value.  Below, the Int() initializer could work for one string, s1, but wouldn't for the second string, s2:

as it's impossible to convert "abc" to an Integer.  Since y needs the option to have no value, we should declare it as an Optional.  We can do so by adding a "?" after the datatype declaration:

which removes the compiler errors.

Another example of when an Optional would be necessary are for properties that cannot be initialized when an object is constructed.  For example:

What's happening here?  Because we're adding the button property, and it's a regular Swift variable, and Swift variables can't store nil values, the compiler gives us the error message "Class ViewController has no initializers".   Again, by declaring button as an optional, by adding the "?" after UIButton, Swift then knows that it could store a nil value.

Using Optionals

Unfortunately, once you declare an Optional, you can't use it unless you find out what it is.  Does it have a nil value or not?  In order to find out, you need to unwrap it first.

If you don't unwrap it, and try to use it, you may see an error like this:

You could just follow the compiler's suggestion to add a "!" to unwrap y, but that does not safely unwrap the variable, and could lead to an app crash if the variable does have a nil value.  To safely unwrap the variable, we'll want to use an "if let" or "if var" statement.  In the example below, we'll be changing the value, so we'll want to use "if var":

We've safely unwrapped the variable, and can handle the case where the value is nil.  The only time you should unwrap using "!" is when you're 100% sure that the value will not be nil.  Otherwise your app with crash.

Optional Chaining

To access the properties of Optional object, you'd normally need to do something like this:

first unwrapping the image using the "if let" statement, then accessing the size property.  With Optional chaining, you're able to unwrap the image object using the "?" notation, and access the size property in one line:

As you can see, this is a slightly more efficient way to unwrap an object and access a property.


  1. The casino might legally place machines of an analogous type payout and advertise that some machines have one hundred pc return to participant. The added benefit is that these massive jackpots enhance the thrill of the opposite gamers. It gives you a large quantity of in-game currency to play with at first. That ought to 우리카지노 help you play for fairly some time earlier than needing a refill. It otherwise boasts plenty of the same options as the opposite slot video games on Android.

  2. For this project, want to|you should|you have to} modify three trays and print the supports appropriately. This is a helpful concept for offices, house studios, and library employees. Also, you'll be able to|you probably can} optimize your Men’s Puffer Jackets portfolio and get helpful products that you could give away or use for your self. 3D prosthetics may be be} one of the essential advances in this field. Many organizations and corporations in the well being business work to offer these types of|these kind of|most of these} tasks to folks in need.