PRO SWIFT
BOOK AND VIDEOS
Break out of beginner’s Swift
with this hands-on guide
Paul Hudson
Contents
Preface
Welcome
Chapter 1: Syntax
Pattern matching
Nil coalescing
Guard
Lazy loading
Destructuring
Labeled statements
Nested functions, classes and structs
Documentation markup
Chapter 2: Types
Useful initializers
Enums
Arrays
Sets
Tuples
Generics
Chapter 3: References and Values
What's the difference?
Closures are references
Why use structs?
Why use classes?
Choosing between structs and classes
Mixing classes and structs
Immutability
Chapter 4: Functions
Variadic functions
Operator overloading
Closures
The ~= operator
4
9
64
102
122
www.hackingwithswift.com
2
Chapter 5: Errors
Error fundamentals
Error propagation
Throwing functions as parameters
try vs try? vs try!
Assertions
Chapter 6: Functional programming
What is functional programming?
map()
flatMap()
filter()
reduce()
sort()
Function composition
Lazy functions
Functors and monads
Chapter 7: Patterns
Object-oriented programming
Protocol-oriented programming
MVC
MVVM
Command-line Swift
149
170
206
www.hackingwithswift.com
3
Preface
Andy Matuschak (@andy_matuschak), lead mobile developer at Khan Academy
If you’re transforming a collection, but you don’t need to access all the elements right away (or
at all), you may be able to save cycles and allocations by using the lazy family of collection
types:
let userCellsData = users.lazy.map { user in
UserCellData(username: user.username, bioAttributedString:
formatBioString(user.bio))
}
www.hackingwithswift.com
4
Welcome
This is not a book for people who are new to Swift. I realize that's obvious given that the book
is called Pro Swift, but I want to make sure we're getting off on the right foot. Instead, this is a
book for people who have some real-world knowledge of the language – perhaps you've made
some apps yourself, perhaps you've read through Apple's official Swift reference guide, or
perhaps you even read my first book, Hacking with Swift.
It is not required that you have read Hacking with Swift in order to make the most of this book,
but I have to admit it certainly helps. At the very least you should have read the introduction
chapter, which goes through the basics of Swift in about an hour of coding. If you haven't
already read that chapter, I suggest you stop reading, go and buy Hacking with Swift, and read
it now – that link gives you $5 off as a thank you for buying Pro Swift.
If you're still here, I'm assuming it means you're ready to learn about what's in this book. Well,
I have some good news: I have distilled 50,000 words of knowledge into this little volume, all
of which is dedicated to helping you write your best Swift code. I've tried to cover a wide
range of topics so that everyone will find something of interest, which means you'll find
chapters on closures, chapters on functional programming, chapters on MVC, chapters on
operator overloading, and more – it's a real feast for developers who want to see what Swift is
truly capable of.
I have purposefully gone way beyond the original chapter list I originally announced, partly
because I enjoy giving people more than they expected, but mostly because working with
advanced Swift is so much fun. I have to admit that it's easy to slip into writing "Objective-C
in Swift" when you're an experienced Apple developer, at which point the language just seems
like a bossy school teacher that enjoys pointing out your mistakes. But once you start writing
idiomatic Swift – Swift as it was meant to be written – suddenly you realize you have an
elegant, efficient, and expressive language that will help you ship better software.
There is no right way to read Pro Swift. You can jump in at whatever chapter interests you if
that's what works, although I suspect you'll find it easier just to start at the beginning and work
your way through. I make no apologies for jumping around so many topics – I've tried to be as
precise as possible while still giving you hands on examples you can try as you read. And
please do try the examples!
www.hackingwithswift.com
5
One more thing: obviously I hope you enjoy all the chapters in the book, but there were a few
that I particularly enjoyed writing and I hope you enjoy them as much while reading. They are
"Labeled statements", "Throwing functions as parameters", "flatMap()", and "Function
composition" – have fun!
Frequent Flyer Club
You can buy Swift tutorials from anywhere, but I'm pleased, proud, and very grateful that you
chose mine. I want to say thank you, and the best way I have of doing that is by giving you
bonus content above and beyond what you paid for – you deserve it!
Every book contains a word that unlocks bonus content for Frequent Flyer Club members. The
word for this book is CYCLONE. Enter that word, along with words from any other Hacking
with Swift books, here: https://www.hackingwithswift.com/frequent-flyer
Dedication
This book is dedicated to my nephew John, who is right now taking his first steps in
programming. He's a smart kid with a lot of ideas, but most importantly he's massively curious
about the world around him. I hope one day not too far in the future this book will be useful to
him too.
Acknowledgements
A number of people were kind enough to contribute to the creation of this book, and I'm most
grateful. In particular, some really great developers took the time to write a short snippet to
open each chapter of the book, giving you some fresh, practical insights from Swift developers
I respect deeply. In alphabetical order they are Wayne Bishop, Chris Eidhof, Matt Gallagher,
Simon Gladman, Wendy Lu, Andy Matuschak, Natasha Murashev, and Veronica Ray – thank
you all!
I'm also grateful to Michael Mayer, who helps run the Philly CocoaHeads book club, for
providing a second opinion on a chapter I was doubtful about. I ended up removing it from the
book – as much as I hate deleting work I've written, I would hate it even more if I was giving
you something I thought wasn't up to scratch.
www.hackingwithswift.com
6
you something I thought wasn't up to scratch.
In places I have used small amounts of source code from the official Swift open source project,
mostly to illustrate specific techniques. This code is released under the Apache License v2.0
with the following header:
Copyright (c) 2014 - 2016 Apple Inc. and the Swift project
authors
Licensed under Apache License v2.0 with Runtime Library
Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift
project authors
The Swift development team continue to do incredible work. My job – and yours! – would be
very different without them, and it's important we thank them for their continuing efforts. In
particular, Joe Groff (@jckarter) seems to have more hours in the day than anyone else, and
I'm grateful for all the times he's answered my questions.
Get in touch
As this is an early release of a new book, I would love to hear your feedback. If you spot any
typos or other mistakes, please email me at paul@hackingwithswift.com or tweet me
@twostraws and I'll do my best to correct quickly. I also intend to write new chapters in the
coming weeks, so please don't hesitate to get in touch with special requests.
Thank you for your support – I love programming in Swift, writing about Swift, and teaching
others about Swift. Your support is what makes that possible, and it means a lot to me.
Copyright
Swift, the Swift logo, Xcode, Instruments, Cocoa Touch, Touch ID, AirDrop, iBeacon, iPhone,
iPad, Safari, App Store, Mac and OS X are trademarks of Apple Inc., registered in the U.S. and
other countries.
Pro Swift and Hacking with Swift are copyright Paul Hudson. All rights reserved. No part of
this book or corresponding materials (such as text, images, or source code) may be reproduced
www.hackingwithswift.com
7
or distributed by any means without prior written permission of the copyright owner.
www.hackingwithswift.com
8