Maybe It’s There, Maybe It Isn’t: Optionals

A few times so far, we’ve seen our result values log messages include the term “optional,” a behavior we haven’t explained yet. But it’s time to deal with it, because optionals are one of Swift’s defining features. Create a new playground called OptionalsPlayground and delete the "Hello, playground" line, as usual.

We’ll start by adding the sizeInMm dictionary from a few sections back, since that’s something that started giving us this “optional” stuff.

 let​ sizeInMm = [
 "iPhone 7"​: 138.1,
 "iPhone 7 Plus"​ : 158.1,
 "iPad Air 2"​ : 240.0,
 "iPad Pro"​ : 305.7]

Looking at this, we can see that sizeInMm["iPhone 7"] should evaluate ...

Get iOS 10 SDK Development, 1st Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.