Swift Developers Book
Swift Developers Book
The
The Swift Developer’s Cookbook Web Edition
Swift
™
as part of the Content Update Program:
Developer’s Cookbook If you purchased your book from informit.com, your free Web Edition can be found
under the Digital Purchases tab on your Account page.
If you purchased this book elsewhere, you need to register your book online to access
your free Web Edition. If you have not registered your book, follow these steps:
1 Go to informit.com/register.
This book is part of Addison-Wesley and InformIT’s exciting 2 Sign in or create a new account.
new Content Update Program, which provides automatic content
3 Enter ISBN: 9780134395265.
updates for major technology improvements!
4 Answer the questions as proof of purchase.
4 As Apple makes significant updates to the Swift 2 language,
sections of this book may be updated or new sections added. 5 Click on the Digital Purchases tab on your Account page to access
your free Web Edition.
4 Updates are delivered via a free Web Edition of this book,
which can be accessed with any Internet connection.
4 This means your purchase is protected from immediately More About the Content Update Program…
outdated information!
For more information on InformIT’s Content Update program, see the inside Addison-Wesley may update The Swift Developer’s Cookbook Web Edition periodically
back cover or go to informit.com/CUP. if Apple introduces significant updates to the Swift 2.x language.
Registered users will receive an email alerting them of the changes each time The Swift
Developer’s Cookbook Web Edition has been updated. The email alerts will be sent to the
email address used for your informit.com account.
When a new edition of this book is published, no further updates will be added to this
book’s Web Edition. However, you will continue to have access to your current Web Edition
with its existing updates.
The Web Edition can be used on tablets that use modern mobile browsers. Simply log into
your informit.com account and access the Web Edition from the Digital Purchases tab.
If you have additional questions, please email our Customer Service department at [email protected]. For more information about the Content Update Program, visit informit.com/CUP or
email our Customer Service department at [email protected].
The Swift™
Developer’s
Cookbook
This page intentionally left blank
The Swift™
Developer’s
Cookbook
Erica Sadun
Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town
Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City
Sao Paulo • Sidney • Hong Kong • Seoul • Singapore • Taipei • Tokyo
Preface xiii
How This Book Is Organized xiii
About the Sample Code xiv
Contacting the Author xv
3 Optionals?! 43
Optionals 101 43
Unwrapping Optionals 45
Forced Unwrapping 45
Conditional Binding 46
Conditional Binding and Type Casts 46
Binding Cascades 48
guard Statements 50
Implicitly Unwrapped Optionals 51
Guarding Failable Initializers 52
Optionals and Sentinel Values 53
Coalescing 55
Optional Assignment 55
Optional Patterns 56
Optional Chaining 58
Selector Testing and Optional Chaining 59
Subscripts 60
Optional Mapping 61
Maps and Chaining 61
Filtering nil Values with flatMap 62
Unmanaged Wrappers 62
Wrap-up 64
Currying Costs 94
Currying and Callbacks 94
Practical Currying 95
Passing Closures as Function Parameters 97
Wrap-up 99
6 Errors 125
Failing Hard 125
Fatal Errors 126
Assertions 126
Preconditions 127
Aborting and Exiting 128
Failing Gracefully 128
The ErrorType Protocol 129
Choosing Between Optionals and Error Handling 130
Swift Error Rules 130
Rule 1: Move Away from nil Sentinels 131
Rule 2: Use throw to Raise Errors 132
Rule 3: Use Error Types with Visible Access 133
Rule 4: Mark All Error-Participating Methods with
throws 134
Rule 5: Use rethrows Consistently 134
Rule 6: Consume Errors Where They Matter 135
Rule 7: Terminate Threaded Error Chains 135
Building Errors 137
Good Errors 138
Naming Your Errors 138
Adding String Descriptions 139
Adding Reasons 139
Simplifying Output 140
Extending String 140
Type-Specific Errors 141
Retrieving Context 141
Contextualizing Strings 142
Contextualizing Throwing Types 143
Simplifying Contexts 144
Calling Throwing Functions 145
Using try 145
Error Propagation 147
Using try! 148
Using try? 148
7 Types 155
Language Concepts 155
Reference and Value Types 155
Copy-and-Write-Back 156
Algebraic Data Types 157
Other Terminology 158
Enumerations 159
Basic Enumerations 159
Using Hash Values to Support Ranges 160
Raw Value Enumerations 162
Raw Value Members and Sequences 163
Associated Values 164
Indirect Values 165
Switches 166
Branching 166
Breaks 167
Fallthroughs 167
Complex Cases 169
Tuples 169
Pattern Matching with Value Bindings 169
where Clauses 170
Unwrapping Optional Enumerations 171
Embedding Values by Type 172
Option Sets 174
Revisiting NS_OPTIONS 174
Building Enumerations 175
Building Option Sets 176
Viewing Options 176
Classes 177
Optimization 178
Initializers 178
Initialization Steps 179
Designated and Convenience Initializers 179
Initialization Rules 179
Building Convenience Initializers 181
Failable and Throwing Initializers 181
Deinitializers 183
Property Observers 184
Getters/Setters and Access-Level Modifiers 185
Extensions and Overrides 186
Lazy Evaluation 187
Lazy Sequences 187
Lazy Properties 188
Wrap-up 188
8 Miscellany 189
Statement Labels 189
Custom Operators 190
Declaring Operators 191
Conforming with Operators 192
Evaluating Operator Trade-offs 192
Array Indexing 193
Multi-indexed Array Access 194
Wrapped Indices 195
Array Slices 195
General Subscripting 196
Parameter-less Subscripting 197
String Utilities 198
Repeat Initializers 198
Strings and Radix 198
String Ranges 199
String Splitting 201
String Subscripts 201
Index 207
Swift is simply a joy to program in. Its constructs and libraries present you with new ways
to craft code, handle data, and perform the endless daily tasks that make up a programmer’s
life. From protocol-oriented and functional programming to first-class closures and algebraic
data types, Swift offers a fresh and exciting take on programming. The more time you spend
developing in Swift, the harder it becomes to return to older languages that don’t offer these
powerful features.
This book is not a traditional tutorial. It’s written for programmers both experienced and new
who are looking to push existing aptitudes into a new arena. Each concept-focused chapter
covers a practical skill set. These chapters guide you to mastery over those essential Swift
development tasks. You needn’t read this cookbook from cover to cover (although you are
more than welcome to do so). Instead, dive into whatever topic you want to learn about and
uncover key insights to bring away with you from each discussion.
The Swift Developer’s Cookbook has been an amazing project to work on. I hope you enjoy
reading this book half as much as I’ve enjoyed working on it.
Retrieve sample code either by using git tools to clone the repository or by clicking GitHub’s
Download button. It was at the right center of the page when I wrote this book. It enables you
to retrieve the entire repository as a ZIP archive or tarball.
Contribute!
Sample code is never a fixed target. It continues to evolve as Apple updates its Swift language.
Get involved. Pitch in by suggesting bug fixes and corrections and by expanding the code that’s
on offer. GitHub allows you to fork repositories and grow them with your own tweaks and
features and then share them back to the main repository. If you come up with a new idea or
approach, let me know.
Getting GitHub
GitHub (http://github.com) is the largest git-hosting site, with more than 150,000 public
repositories. It provides both free hosting for public projects and paid options for private
projects. With a custom Web interface that includes wiki hosting, issue tracking, and an
emphasis on social networking of project developers, it’s a great place to find new code or
collaborate on existing libraries. Sign up for a free account at the GitHub website, which then
allows you to copy and modify this repository or create your own open-source iOS projects to
share with others.
My gratitude extends to everyone who helped read through drafts and provided feedback.
Specific thanks go out to Ken Ferry, Jeremy Dowell, Remy Demarest, August Joki, Mike Shields,
Phil Holland, Mike Ash, Nate Cook, Josh Weinberg, Davide De Franceschi, Matthias Neeracher,
Tom Davie, Steve Hayman, Nate Heagy, Chris Lattner, Jack Lawrence, Jordan Rose, Joe Groff,
Stephen Celis, Cassie Murray, Kelly Gerber, Norio Nomura, “Eiam,” Wess Cope, and everyone
else who contributed to this effort. If I have omitted your name here, please accept my
apologies.
Special thanks also go to my husband and kids. You guys are the best.
You can email or write me directly to let us know what you did or didn’t like about this book—
as well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this book,
and that due to the high volume of mail we receive, we might not be able to reply to every
message.
When you write, please be sure to include this book’s title and author as well as your name and
phone or email address. I will carefully review your comments and share them with the author
and editors who worked on the book.
Email: [email protected]
Reader Services
Register your copy of The Swift Developer’s Cookbook at informit.com for convenient access
to downloads, updates, and corrections as they become available. To start the registration
process, go to informit.com/register and log in or create an account*. Enter the product ISBN,
9780134395265, and click Submit. Once the process is complete, you will find any available
bonus content under Registered Products.
*Be sure to check the box that you would like to hear from us in order to receive exclusive
discounts on future editions of this product.
nil happens. When dictionary lookups fail, when instance properties are case dependent,
when asynchronous operations have not completed, when failable initializers cannot create
instances, and in dozens of other situations where values may or may not have been set, Swift
may return nil instead of some other more concrete content. Swift provides nil as a powerful
tool for expressing situations in which values are unavailable for use.
Swift differentiates these “no content” scenarios from error handling. In error handling, control
flow changes to offer failure mitigation and reporting. With nil, a value-of-no-value repre-
sents an absence of data. It offers a testable placeholder to be used when no data is otherwise
available.
Unlike in many other languages, in Swift, nil is not a pointer. It is a safe and expressive way to
represent the potential for both a valid and invalid value within a single construct. In Swift, the
Optional type encapsulates this concept and enables you to differentiate between successful
value assignments and nil cases.
Learning how to recognize and use optionals is an essential step in mastering the Swift
language. This chapter introduces optionals and surveys the supporting constructs you need to
create, test, and successfully use optionals in your code.
Optionals 101
Question marks and exclamation points are the hallmark of Swift optionals. Any variable
marked with ? may—or may not—contain a valid value. To get a sense of this language feature,
consider the following example:
var soundDictionary = ["cow": "moo", "dog": "bark", "pig": "squeal"]
print(soundDictionary["cow"] == "moo") // prints true
print(soundDictionary["fox"]) // What does the fox say?
What does the fox say? In Swift, the answer is nil. In Swift, nil indicates “no value.”
Unlike in Objective-C, in Swift, nil is not a pointer. Swift nil indicates a semantic missing
non-existence, a count of Monte Cristo, a cup of “not tea,” an honest politician. A nil item
means “nothing to see here, move along, move along, move along.”
In this soundDictionary example, the variable stores a string dictionary. With strings for both
keys and values, its type is Swift.Dictionary<Swift.String, Swift.String>. You can also
represent this type as [String: String], using square brackets and a colon. Swift infers this
type from the concrete data provided on the right-hand side of the soundDictionary assign-
ment. Alternatively, you can use explicit type annotation in your code by adding a colon and a
type in the variable declaration:
var soundDictionary: [String: String] =
["cow": "moo", "dog": "bark", "pig": "squeal"]
Although the dictionary uses String keys and values, when you look up any item in this
dictionary, the value returned is not a String. It’s typed String?. That question mark is criti-
cal to understanding dictionaries because it indicates an Optional type. Dictionary lookups
may succeed or fail. Optional return types encapsulate both possibilities.
Contrast this behavior with arrays, where it’s the programmer’s job to check whether an index
exists before accessing it. Both types could easily be implemented with the other convention,
of course, but the Swift people chose the more likely use case for each type. Arrays are highly
bounded with a small domain of legal lookup indexes. Dictionaries are often sparse compared
against their possible key domain. Optionals enables dictionaries to better represent their “may
or may not map to a value” results.
Confirm the return type with Quick Help. Enter the dictionary and the following assignment
in Xcode. Then Option-click the sound symbol (as in Figure 3-1) or select the symbol and open
View > Utilities > Show Quick Help Inspector. The declaration line in the Quick Help presenta-
tion confirms that the type assigned to sound is String?:
var sound = soundDictionary["cow"] // sound is typed String?
While it might appear that the dictionary returns "moo" on success or nil for failed lookups,
this is actually misleading. Print the output to the Xcode console, with print(sound). A
successful result looks like Optional("moo"), with its value embedded in an optional wrapper.
In this case, the Optional type uses a .Some enumeration, and its associated value is the
"moo" string.
You cannot use this optional directly as a string. The following example fails because you must
unwrap Optional types before using their values:
let statement = "The cow says " + sound
The + operator in this case works on two string values, not on a string and an optional.
Unwrapping gives you access to the string stored within the optional wrapper, enabling you to
perform tasks like appending values.
Unwrapping Optionals
Optional types always return either a wrapped value or nil. Wrapping means that any actual
content is stored within a logical outer structure. You cannot get to that value (in this case,
"moo") without unwrapping it. In the Swift world, it is always Christmas, and there are always
presents—or at least variables—to unwrap.
Swift offers a variety of mechanisms for unwrapping optionals and recovering the underlying
data type stored within them. There are more ways to get at a wrapped optional value than you
might expect. The expressive possibilities expanded in Apple’s Swift 2 update to provide greater
developer flexibility.
Forced Unwrapping
The simplest way to extract an optional value is to append an exclamation point:
print("The sound is \(sound!)") // may or may not crash
This is also the most dangerous approach, and you want to generally avoid it in production
code.
If the value is wrapped (that “optional” result you just read about), the exclamation point
returns the value stored within the wrapped element. If the value is nil, you experience a fatal
runtime error—which is not nearly as fun or gratifying for your workday.
The nil value is the Swift equivalent of your aunt’s home-crocheted vest with a large
friendly moose decoration on it. It’s pretty important to check whether an item is nil before
unwrapping it. You can more safely unwrap with an exclamation point within an if state-
ment because this sound is nominally guaranteed to be non-nil in non-extreme (that is,
nonthreaded) situations:
if sound != nil {
print("The sound is \(sound!)") // doesn't crash
}
Safety is a relative thing. While this forced unwrap is pretty safe at compile time and runtime
in its current use, it’s dangerous while editing. If you accidentally move that print line out of
the if block, the compiler cannot warn you about it, and you will ship broken code.
In this example, the sound variable is also not weak. It won’t deallocate between the nil test
and unwrapping. Never use forced unwraps with weak variables because they may do just that.
As a rule, avoiding forced unwraps, even within if clauses, helps you avoid potential disasters.
Conditional Binding
Conditional binding provides a better and safer approach that avoids forced unwrapping. In
conditional binding, you optionally bind (that is, assign) a value to a new constant or variable
using an if-let or if-var statement. When the optional is not nil, Swift unwraps the value,
assigns it, and executes a block of code. When nil, you either enter an else clause or continue
to the next statement:
if let sound = soundDictionary[animal] {
print("The \(animal) says \(sound)")
} else {
print("Any sound the \(animal) says is unknown to modern science")
}
It’s conventional in Swift to use identical identifiers in conditional bindings for both wrapped
and unwrapped versions. In the following snippet, a mySound constant is unwrapped within
the if-let (or if-var) scope but wrapped outside it:
let mySound = soundDictionary[animal] // mySound is wrapped
if let mySound = mySound {
print(mySound) // mySound is unwrapped
}
Using the same identifier makes it clear that you’re working with the same semantics in differ-
ent states of undress. Conditional binding clauses tend to be short. This enables you to verify
your overloaded symbol intent with a glance.
The right-hand value in an if-let assignment must be an optional. You cannot use this
construct with an already-unwrapped item. The following statement leads to an error,
complaining that a conditional binding initializer must have Optional type:
if let sound = soundDictionary["cow"]! { // error!
print("The cow says \(sound)")
}
let value = 3
value is Any // true
value is Int // true
value is String // false
The as operator casts to other types and can test whether an item conforms to a protocol.
These are its three forms:
■ The as operator applies direct casts. The compiler must determine at compile time that
the cast will succeed.
■ The as? variant contains a question mark. It performs a conditional cast that always
returns an optional value. If the cast succeeds, it’s wrapped in an optional instance. If
not, it returns nil. This is the cast I use most often in day-to-day coding. The compiler
issues warnings when it detects that a cast will always succeed (for example, String to
NSString) or always fail (String to NSArray).
■ The forced variant as! includes an exclamation point. It either returns an unwrapped
item or raises a runtime error. This is the most dangerous cast type. Use it when you
know a cast will always succeed or when you want your application to prematurely crash.
Conditional binding and conditional casting work hand-in-hand. Figure 3-2 demonstrates
why. In this screenshot, Swift code creates a string-indexed dictionary that stores arbitrary
Any instance types. Even when a lookup returns a valid integer for the "Three" key, a forced
unwrap still reports Any type. To use this value as a number, you must downcast from Any
to Int.
Figure 3-2 Lookups from a [String:Any] dictionary return Optional<Any> values. The type
remains Any even after forced unwrapping.
The as? type operator produces optionals even when used with non-optional values:
let value = 3 // Int
let optionalValue = value as? Int // Int?
Conditional binding enables you to apply a cast, test the resulting optional, and then bind
unwrapped values to a local variable. In the following example, result is bound as an Int
constant within the if clause code block:
if let result = dict["Three"] as? Int {
print("\(result + 3) = 6") // 6 = 6
}
This conditional pattern of fetch, cast, and bind commonly appears when working with
structured data from web services and databases. Swift enables you to use type safety while
navigating through key paths down to data leaves.
Recipe 3-1 searches a UIView hierarchy, using conditional casting to match subviews against a
supplied type, returning the first subview that matches that type. The as? type operator returns
an optional, which is conditionally bound to a local view variable. If the binding succeeds and
a type match is found, the function returns. Otherwise, it recurses down the tree in a depth-
first search.
Binding Cascades
Although useful for accessing values, if-let bindings build into code structures colloquially
known as pyramids of doom. Each successive test indents your code further and further to the
right, creating a triangle of empty space to the left of the if statements and then the closing
brackets:
if let cowSound = soundDictionary["cow"] {
if let dogSound = soundDictionary["dog"] {
if let pigSound = soundDictionary["pig"] {
// use sounds here
}
}
}
It’s the sort of code you don’t want to bring home to your project manager—or your mummy.
Using multiple optional bindings within a single test enables you to avoid this structural mess.
First introduced in Swift 1.2, cascading permits any number of conditional bindings tests
within a unified conditional:
if let
cowSound = soundDictionary["cow"],
dogSound = soundDictionary["dog"],
pigSound = soundDictionary["pig"] {
// use sounds here
}
If any assignment fails, evaluation stops, and control moves past the if statement. This
approach is particularly useful when working with web-sourced data, such as JSON, where each
step may depend on the results of the previous conditional binding. The following example is
from a script that checks prices on the iTunes App Store:
if let
json = json as? NSDictionary,
resultsList = json["results"] as? NSArray,
results = resultsList.firstObject as? NSDictionary,
name = results["trackName"] as? String,
price = results["price"] as? NSNumber {
// Process results
}
This code pulls data from the App Store and deserializes it with NSJSONSerialization.
JSONObjectWithData. The results list is extracted from the JSON data, and the app results
from that list, and so forth.
While this approach avoids pyramid indentation, it introduces a bad tendency toward code
constipation. Increased blockiness makes if-let cascades hard to mentally process. Copious
commenting and spacing can mitigate this. As shown here, the results are faster for the brain
to parse, make it easier to spot errors, and are better for adding, removing, and reordering inter-
mediate steps:
if let
// Access JSON as dictionary
json = json as? NSDictionary,
// Process results
}
guard Statements
The guard statement was first introduced in Swift 2. It offers another way to unwrap and use
optionals. Although guard is not limited to use with optionals—you can use it with general
Boolean conditions as well—it provides a major development advantage that’s specific to
optional handling. In particular, guard statements move code out of indented scopes by offer-
ing early-return error handling.
Early-return inverts the handling of success and failure paths. Errors are handled first, enabling
success code to be written at the highest possible scope. Compare this to if-let, where you
cannot put the failure path first and your success conditions must be handled in a child scope.
Without guard statements, all conditionally bound values must be used with the if state-
ment code block that binds them. For example, the following snippet conditionally binds a
cowSound constant using if-let, and the cowSound constant is undefined outside that scope:
if let cowSound = soundDictionary["cow"] {
// cowSound is unwrapped within this scope
}
// cowSound is undefined outside the scope
Like if-let, a guard statement conditionally unwraps and binds optional variables. When
that binding fails, it executes a mandatory else clause, which must exit the current scope,
using throw, break, continue, or return statements. Otherwise, the guard statement
unwraps the optional value and binds it to a variable you can use throughout the remaining
lifetime of the current scope:
guard let cowSound = soundDictionary["cow"] else {throw Error.MissingValue}
// cowSound now defined and unwrapped
Think of a guard statement as a soldier that will not permit the flow of execution to continue
forward unless its condition or conditions are met. Unlike if-let, guard does not establish
new scopes. Instead, it guides traffic through the existing scope.
Like if-let, a single guard statement can express multiple conditions, separated by comma
delineators. For example, you might perform multiple constant assignments:
guard let
cowSound = soundDictionary["cow"],
dogSound = soundDictionary["dog"]
else {throw Error.MissingValue}
Here’s an example of first an assignment to a normally typed constant and then to an implic-
itly unwrapped one. The difference is what happens when you access the wrapped value. That’s
where the name comes from because during access, the value is implicitly unwrapped:
var animal = "cow" // 1
let wrappedSound = soundDictionary[animal] // Optional("moo")
let unwrappedSound: String! = soundDictionary[animal] // "moo"
In this example, when you use unwrappedSound in the string assignment, its value is not
Optional("moo"). Its value is accessed as moo, and it behaves like a simple string. Implicit
unwrapping extracts the value from an optional item and uses the data type you’d expect from
a successful dictionary lookup. Once unwrapped, you can use the variable directly. You don’t
have to add that “unwrap this variable” exclamation point or use conditional binding.
A real danger arises from unwrapping nil values. If you replace the assignment to animal
in the line marked 1 with "fox" instead of "cow", this code raises a fatal error in the line
marked 2. The runtime complains that it unexpectedly found nil while unwrapping an
Optional value. With great unwrapping comes great responsibility. Take care that you prop-
erly guard these items to ensure that you don’t attempt to unwrap nil.
Limit implicit unwrapping to when you know in advance that a variable will always have a
value after some logical point. For example, if you’re responding to button taps or menu clicks,
you probably don’t have to wonder “does this button or menu item exist?” It does. If it didn’t,
you would never have reached your callback.
Don’t use implicit unwrapping for more general cases like dictionary lookups. That’s just asking
for trouble. You can, however, print unwrapped items and test them against nil:
print(unwrappedSound) // prints nil
if unwrappedSound != nil {
print("Valid:", unwrappedSound)
}
An error won’t be raised until you perform an operation that attempts to access the value
inside.
As a rule, it is legal to assign a value, an optional value, or nil to an implicitly unwrapped vari-
able. The following code is legal and will compile without error:
var myImplicitlyUnwrappedOptional: String!
myImplicitlyUnwrappedOptional = Optional("Value")
myImplicitlyUnwrappedOptional == "Value" // true
myImplicitlyUnwrappedOptional = nil // do not test except against nil
myImplicitlyUnwrappedOptional == nil // true
myImplicitlyUnwrappedOptional = "Value"
myImplicitlyUnwrappedOptional == "Value" // true
Assignments to both optional and non-optional values end with non-optional results, which
you see by comparing against the non-optional string literal "Value". The danger lies in the
nil assignment. A non-nil comparison tries to access the value and ends in a runtime error.
The win is that you can perform an assignment to either optional or non-optional values with
a single assignment.
An implicitly unwrapped scenario is common when working with Interface Builder. A view
binding is optional; it may or may not be properly instantiated in the interface file. The view
use is normally non-optional; presumably you set up the binding correctly. Implicit unwrap-
ping simplifies code access to views at the cost of potential crashes during development and
testing.
Swift 2’s one-line guard statement more or less offers a shorthand summary for the following
initialization pattern:
let cowSound: String! = soundDictionary["cow"]
if cowSound == nil {throw Error.missingValue} // Handle nil case
// cowSound's value is now usable without explicit unwrapping
This assignment uses implicit unwrapping and then tests against nil. This is a safe check
because the code makes no attempt to access an associated value. If the optional is nil, the if
statement’s conditional block executes, transferring control away from this scope. Otherwise,
execution continues, and the unwrapped cowSound constant is now available for use.
An implicitly unwrapped approach is both wordier and less safe than guard. It uses multiple
statements compared with guard’s one, and it makes no conditions about leaving the current
scope. If the throw request were replaced with a print statement, the nil case would continue
forward. In doing so, it might encounter a use of cowSound’s nonexistent value and a nasty
runtime crash.
struct EvenInt {
let number: Int
init?(_ number: Int) {
if number % 2 == 0 {self.number = number} else {return nil}
}
}
Mark failable initializers by appending a question mark or an exclamation point to the init
keyword (init? or init!). The punctuation choice depends on whether the initializer returns
a normal optional instance or an implicitly unwrapped one. The implicitly unwrapped varia-
tion is almost never used in real life. According to my technical editor Kevin Ballard, you might
theoretically encounter one when working with legacy code ported from Core Foundation or
from any unaudited Objective-C API.
Use guard to test assignments from failable initialization. Here’s an example built around the
EvenInt struct. When created with an odd number, an EvenInt initializer returns nil. The
guard statement throws an error and exits the scope:
do {
guard let even2 = EvenInt(2) else {throw Error.Odd}
print(even2) // prints EvenInt(number: 2)
guard let even3 = EvenInt(3) else {throw Error.Odd} // fails
print(even3) // never gets here
} catch {print(error)}
Although you can use any approach for testing and unwrapping optionals, guard statements
offer a natural synchronicity with failable initializers. Guarding initializers once again enables
you to test for failed initialization at the point where you declare variables and constants and
ensures that these bindings remain valid for the remaining scope.
Starting in Swift 2, reserve nil failures for initialization (although you can also use throws in
initializers as well as in normal code) and prefer guard over if-let. Instead of using option-
als as sentinels—that is, to signal success and fail conditions—use Swift’s new error-handling
system. Error handling enables you to redirect the flow of control to mitigate errors and
provide recovery support:
func betterDoSomething() throws -> String {
let success = (arc4random_uniform(2) == 1) // flip a coin
if success {return "success"} // succeed
throw Error.failure // fail
}
do {
let result = try betterDoSomething()
} catch {print(error)}
This refactoring skips optionals; the nil case is never of interest to the client code. Swift 2 error
handling means you never have to unwrap.
When a thrown error is not of interest to its consumer, the try? operator can discard errors
and convert results to optionals. This lets you integrate new-style errors with old-style optional
handling. A try? expression returns a wrapped value for successful calls and nil for thrown
errors:
guard let foo = try? somethingThatMayThrow else {
// ...handle error condition and leave scope
}
if let foo = try? somethingThatMayThrow {}
The new error-handling system profoundly affects Cocoa-sourced APIs. Calls that used NSError
pointers pre-Swift 2 change their return type from optional to non-optional, add the throws
keyword, and eliminate error pointers from API calls. The new system sends NSError through
do-try-catch. Compare the old approach with the new one:
// Old
func dataFromRange(range: NSRange,
documentAttributes dict: [NSObject: AnyObject],
error: NSErrorPointer) -> NSData?
// New
func dataFromRange(range: NSRange,
documentAttributes dict: [String: AnyObject]) throws -> NSData
By introducing error handling, optionals can eliminate overloaded “failed call” semantics. It’s
always better to use throws with well-defined errors than to use optional sentinel values. When
you really have no information to pass back to the caller other than “I failed,” Swift 2’s updated
error system simplifies creating an error enumeration to explain why that failure occurred. It is
ridiculously easy to add informative errors that don’t require complicated NSError initializers:
enum Error: ErrorType {case BadData, MemoryGlitch, ItIsFriday}
Although many current APIs, especially asynchronous handlers and calls based on Core
Foundation, have yet to transition to the new system, I encourage you to update your code to
avoid using optionals as sentinel values. Return your optionals to the “contains a value or does
not contain a value” semantics they were designed to handle.
Coalescing
Swift’s nil-coalescing operator ?? unwraps optionals and provides fallback values for the nil
case. This next example checks uses nil-coalescing to assign a value to the sound constant:
let sound = soundDictionary["fox"] ?? "unknown"
If the lookup succeeds and the dictionary returns a wrapped value, the operator unwraps it and
assigns it to sound. If not, it assigns the fallback value, which is "unknown". In either case, the
assignment is not optional. sound uses a String type, not String?.
Use nil coalescing when you can supply fallback values that won’t interrupt a normal opera-
tional flow. When you cannot, choose guard instead and handle nil as an error case in the
else clause.
Note
If the optional is non-nil, the right-hand side of the operator is never evaluated. The operation
short-circuits, as with the Boolean operators && and ||.
Optional Assignment
In nil coalescing, you must supply a valid unwrapped fallback value. In cases where a fallback
does not exist, consider optional assignment instead. This approach shortcuts any case where
an optional value is unavailable. Normally, you embed assignments into an if-let scope. If
the conditional binding succeeds, you assign the unwrapped value.
Alternatively, you might consider creating a custom operator that conditionally assigns values
to a target, as in the following example:
// Thanks, Mike Ash
infix operator =? {}
public func =?<T>(inout target: T, newValue: T?) {
if let unwrapped = newValue {
target = unwrapped
}
}
This snippet builds an =? operator that supports simple assignment by wrapping and hiding its
underlying if-let approach with a basic infix call.
The following assignments show this operator in action. The s string variable updates only for
non-nil assignments:
var s: String = "initial value"
s =? "value 1" // value 1
s =? nil // value 1
Hiding if-let overhead makes the resulting conditional assignments cleaner and potentially
more intuitively obvious to read through.
Optional Patterns
A Swift pattern matches a value’s structure rather than just the value itself. Patterns decompose
and express a data structure, including component elements. They enable you to refer to an
item’s subconstructs when testing instead of dealing with instances as a monolithic whole.
Using patterns is a powerful and nuanced way to represent an instance, and it’s extremely
handy for working with optionals.
The optional enumeration consists of two cases, .None and .Some. The .Some case contains an
associated value of an arbitrary type:
enum Optional<T> {
case None
case Some(T)
}
With optionals, pattern matching enables you to limit statements to work only with the .Some
case. You can reach into that case and bind the internal value using a single declaration.
Follow the case keyword with a specific enumeration (the .Some case) and then bind the value
nestled within it using let or var:
switch soundDictionary[animal] {
case .Some(let sound):
print("Using case, the unwrapped sound is \(sound)")
default: break
}
The case .Some(let constant) expression provides a one-to-one pattern match with the
underlying optional. Optionals with the None case don’t match this pattern, so the case code
need not consider nil scenarios.
There’s no denying that the case (case .Some(let constant)) is awkward to process visu-
ally. It lacks grace. Responding to the complexity of this optional pattern-matching code,
Swift 2 introduces shorthand using a postfixed question mark:
switch soundDictionary[animal] {
case let sound?:
print("Using case, the unwrapped sound is \(sound) [2]")
default: break
}
This postfixed question mark is nothing more than syntactic sugar for the .Some case. The
results are simpler and more readable because this case matches and unwraps its optionals.
The preceding example shortchanges Swift because there’s still no major advantage in using
this one-case switch statement over, say, if-let. When you introduce a simple where
clause, you begin to see where pattern-matching optionals adds power and concision.
The following snippet uses a switch statement to differentiate unwrapped optionals whose
string values are longer than five characters from those that are shorter:
switch soundDictionary[animal] {
case let sound? where sound.characters.count > 5:
print("The \(sound) is long")
case let sound?:
print("The \(sound) sound is under 5 characters")
default: break
}
Swift’s switch cases may experience significant logical overlap where small details select which
case to execute.
Using pattern matching with if statements enables you to drop switch statement overhead
and simplify one-case code even further. You lose the default statement and the surround-
ing switch, and you reduce the check to just a simple pattern comparison with where clause
support. This if statement uses both pattern matching and a where clause for precise testing:
if case let sound? = soundDictionary[animal] where sound.hasPrefix("m") {
print("Sound \(sound) starts with m")
}
The where clause isn’t limited to if-case. You can construct a similar statement using if-let:
if let sound = soundDictionary[animal] where sound.hasPrefix("m") {
print("Sound \(sound) starts with m")
}
Pattern matching also enables you to iterate through an array and perform actions on
unwrapped non-nil values. The for-case-let approach simplifies working with optional
collections, as you see in the following snippet:
// Collect sound optionals into array
let soundOptionals = ["cow", "rhino", "dog", "goose", "hippo",
"pig"].map({soundDictionary[$0]})
This example from Apple sample code uses a GameplayKit enumeration. It performs an
enumeration pattern match and binds the targetAgent associated value:
guard case let .HuntAgent(targetAgent) = mandate else {return}
Optional Chaining
In Swift, you chain methods and properties by appending period-delimited selectors. Each func-
tion in the chain returns an intermediate value. This allows calls to be joined into a single
statement without requiring variables that store intermediate results:
soundDictionary.description.characters.count
This approach creates a fluent interface, which is ideally a parsimonious and more readable
expression of a set of operations you want to consider as a single unit. A danger, of course, lies
in over-chaining. If you’re producing enormous lines of code that are difficult to debug and
hard to read and that cannot be easily commented or differentiated on updates, you’re prob-
ably doing this wrong. Ask yourself, “Would I ever need to put a breakpoint in this statement
or step through it?” If the answer is yes, you are over-chaining.
Swift introduces a powerful feature called optional chaining. Swift method calls may return
optionals, and you must take this into account when forming chains. Swift provides a way that
an entire chain can fail gracefully on encountering nil.
Optional chaining involves adding question marks after optional values. For example, you
might look up an animal in the sound dictionary and use optional chaining to return a capital-
ized version of the sound:
soundDictionary[animal]?.capitalizedString // Moo or nil
You can add forced unwrapping to any chain item by replacing the question mark with an
exclamation point. This usage comes with the same forced unwrapping dangers discussed
earlier in this chapter:
soundDictionary[animal]!.capitalizedString // Moo or Runtime Error
Here’s a real-world example of where you might use optional chaining to simplify an if-let
pattern. This code extends Array to return the index of a possible maximum element. Swift’s
standard library maxElement() function returns an optional based on whether a sequence has
values to compare (Apple writes in the standard library, “Returns the maximum element in
`self` or `nil` if the sequence is empty.”):
extension Array where Element:Comparable {
var maxIndex: Int? {
if let e =
self.enumerate().maxElement({$1.element > $0.element}) {
return e.index
}
return nil
}
}
Introducing optional chaining greatly simplifies this code, enabling you to shortcut the index
lookup and returning nil if the maxElement call fails. Recipe 3-2 returns the index of an
array’s maximum value.
Extend Recipe 3-2’s functionality to all collection types with the following snippet:
extension CollectionType where Generator.Element: Comparable {
var maxIndex: Index? {
return self.indices.maxElement({self[$1] > self[$0]})
}
}
Commonly, you work with subclasses that are directly related to each other but that imple-
ment distinct method sets. For example, you might retrieve a collection of SpriteKit nodes from
a scene and then adjust the line widths of the shape nodes. This snippet uses a failable cast
followed by an optionally chained property assignment:
for node in nodes {(node as? SKShapeNode)?.lineWidth = 4.0}
This selector-testing approach also works in pure Swift, as in the following example:
// Root class put two subclasses
class Root {func rootFunc() {}}
class Sub1: Root {func sub1Func() {print("sub1")}}
class Sub2: Root {func sub2Func() {print("sub2")}}
This snippet constructs a heterogeneous array of Root subclasses. It then performs conditional
casts and uses selector tests before calling class-specific methods.
Selector testing enables you to test whether a method exists before constructing a new instance.
Adding the question mark ensures that the NSString call won’t fail with a runtime “unrecog-
nized selector” exception:
let colorClass: AnyClass = UIColor.self
let noncolorClass: AnyClass = NSString.self
colorClass.blueColor?() // returns a blue color instance
noncolorClass.blueColor?() // returns nil
This is a special behavior of AnyClass and AnyObject that works only with Objective-C
methods, for compatibility with Class and id. These are special cases because these types
return functions as implicitly unwrapped optionals. Other types don’t do that.
Subscripts
Contrary to expectations, optional chaining with subscripts doesn’t introduce safe lookups. This
is an important factor that you should internalize as soon as possible and recognize in your
code. In the following example, if you try to access index 8 (aka the ninth element of this six-
element array), your code dies with a fatal Array index out of range error:
let array: Array? = [0, 1, 2, 3, 4, 5]
array?[0] // 0
// array?[8] // still fails
In this example, the question mark does not qualify the lookup for safety. It is required for
subscripting after array, which is optional. With subscripts, you add chain annotations in-line
after the optional value and before the subscript brackets.
Optional chaining is meant solely to set and retrieve values for subscripts used with optional
values. It does not and cannot short-circuit failed subscripts unless you build a failable
subscripting extension, as in the following example:
extension Array {
subscript (safe index: UInt) -> Element? {
return Int(index) < count ? self[Int(index)] : nil
}
}
Once you add a simple array safe-index extension, you can optionally chain the safe version
of the subscript. In the following calls, the Element? results of the safe: subscript are now
optional and can be chained:
print(array?[safe: 0]?.dynamicType) // nil
print(array?[safe: 8]?.dynamicType) // Optional(Swift.Int)
Optional Mapping
Swift’s map and flatMap functions enable you to conditionally apply functions to optional
values. Their calls are similar, as you see in the following declarations, and both are incredibly
useful tools:
/// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.
func map<U>(f: @noescape (T) -> U) -> U?
/// Returns `f(self)!` iff `self` and `f(self)` are not nil.
func flatMap<U>(f: @noescape (T) -> U?) -> U?
The map closures return type U, which may or may not be an optional, while flatMap closures
specifically return type U?, which is always an optional. This practical limitation simply means
you can use map with closures that return non-optionals, but you cannot do the same with
flatMap:
// flatMap must return optional
print(word.map({string->String in string})) // compiles
// print(word.flatMap({string->String in string})) // errors
Both the mapping and if-let include about the same level of code complexity for this partic-
ular example. Both unwrap the optional returned by UIImage(named:) and then print the size.
You can argue which approach is better. Both bind the unwrapped result to a local constant
whether that constant does or does not have an explicit name.
Recipe 3-3 uses a single flatMap call to eliminate nil instances and extract values from their
optional wrappers.
Unmanaged Wrappers
In rare cases (which are growing rarer by the day), a Core Foundation function may return a C
pointer or an object reference embedded in an Unmanaged wrapper. You encounter these in the
older, dustier, and stranger parts of Cocoa, where grues still lurk in shadows. Keychain Services
is a notorious offender in this regard. You must transfer Unmanaged references into the normal
memory management system before working with them.
An Unmanaged wrapper, like an Optional wrapper, provides a layer of safety between your
code and a potentially nasty crash. The Unmanaged<T> type stores a pointer whose memory is
not controlled by the Swift runtime system. Before using this data, you take responsibility for
how this memory should stay alive.
In Cocoa, this works very much like bridging in Objective-C. Unwrap any object with an exist-
ing +1 retain count using takeRetainedValue(). This applies to any item built with Create
or Copy in its name. Use takeUnretainedValue() for +0 returns.
If you have an Objective-C framework or are developing one that you would like people to use
in their Swift application—and if there are methods or functions in your Objective-C frame-
work that return Core Foundation objects—decorate your methods or function names with
CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED. If you don’t decorate your methods or
functions, Core Foundation objects are returned as unmanaged.
Use takeUnretainedValue() to unwrap any object built by a Core Foundation function with
Get in its name (for example, CFAllocatorGetDefault()) and constants passed as unman-
aged objects (for example, kLSSharedFileListSessionLoginItems). These items are not
automatically retained for you. Unlike with takeRetainedValue(), calling takeUnretained-
Value() won’t consume a retain upon unwrapping.
These functions follow the patterns established in Apple’s Memory Management Programming
Guide for Core Foundation, where you can read more about the “create rule,” the “get rule,” and
other details about memory ownership policies. Search the web for the latest update of this
document.
Wrap-up
Optionals are an invaluable component of Swift development. With their “possible value”
semantics, they enable you to store and represent the results of lookup operations whose data
may or may not exist. Optionals are a powerful workhorse construct that you regularly use in
day-to-day operations.
Swift’s new early-return guard statement is a gift that eliminates awkwardness from your code.
Now you can assign, unwrap, and use values with a clear path for missing values. Between
guard and nil coalescing, Swift 2 can elegantly express both fail-on-nil and fallback-on-nil
scenarios with a minimum of overhead and indenting.
Swift 2’s revised error handling has also started to eliminate the role of optionals as sentinel
values. This usage continues to be common as it’s simple to use optionals to represent fail and
success states for method calls. Ideally, optional sentinels will shrivel and die as the language
matures and Apple’s APIs catch up with current language features. Until then, know that it’s
always better to use throws with well-defined errors than to use optional-style sentinels in the
code you control.
Symbols
@objc keyword, 112
? (question mark), 44
optional chaining, 58-61
postfixed question marks, 57
A
abort() function, 128
access control, 159
access-level modifiers, 185-186
adopting protocols, 104-112
algebraic data types, 157-158
aliases, type aliases, 78
alternatives to try? operator, 149-151
Apple Developer Forums, 9
Apple Swift blog, 9
applications, 4
terminating
abort() function, 128
assertions, 126-127
fatalError() function, 126
preconditions, 127-128
arguments. See also parameters
format specifiers, 19-20
positional arguments, 75-76
separator, 13
shorthand argument names, 74-76
tuple arguments, 74
arrays building
comparing with tuples, 73 convenience initializers, 181
multi-index array subscripting, 84, convolution libraries, 92-93
194-195 custom mirrors, 31-34
safe indexing, 193-194 enumerations, 175-176
slices, 195-196 errors, 137-139
typed arrays, 84 functions, 65-72
as operator, 47 parameters, 69
ASCII WWDC, 9 generic types, 113-114
Ash, Mike, 33, 128 header documentation, 35-36
assertions, 126-127 option sets, 176
associated types, 118 protocols, 109
collating, 119-120 with default implementations,
autoclosures, 87-91 110-111
built-in Quick Look types, 27-28
B
Ballard, Kevin, 197
C
basic enumerations, 159-160 callbacks, 94-95
beautification, 117 capture lists, 85-86
best practices capturing values
for protocols, 124 capture lists, 85-86
Swift error handling, 130-137 unowned references, 86-87
annotating with throws keyword, chat rooms, 9
134 classes, 177-184
avoid nil sentinels, 131-132 initializers, 178-184
consuming errors, 135 convenience initializers, 179
error types with visible access, 133 deinitializers, 183-184
throwing errors, 132-133 designated initializers, 179
binding cascades, 48-49 failable initializers, 181-183
blogs, Apple Swift blog, 9 optimization, 178
bracket overload, 117 subclassing, 178
branching, 166-167 closures, 65
breaks, 167 autoclosures, 87-91
capturing values
capture lists, 85-86
unowned references, 86-87
W
websites, Apple Developer Forums, 9
where clauses, 170-171
willSet, 184-185
wrapping indices, 195
X-Y-Z
Xcode
migrating code, 2-4
Quick Look for Custom Types, 24-25