Contents
Preface 8
About this book
Notes for iOS developers
TIMTOWTDI
Introduction: Swift for Complete Beginners 19
How to install Xcode and create a playground
Variables and constants
Types of Data
Operators
String Interpolation
Arrays
Dictionaries
Conditional statements
Loops
Switch case
Functions
Optionals
Optional chaining
Enumerations
Structs
Classes
Properties
Static properties and methods
Access control
Polymorphism and typecasting
Closures
Protocols
Extensions
Protocol extensions
2 www.hackingwithswift.com
Wrap up
Project 1: Storm Viewer 176
Storm Viewer: Setting up
Splitting a window in two
Adding custom controllers
Designing our interface
Introducing outlets
Showing some table rows
Loading our images
Responding to table selection
Finishing touches
Storm Viewer: Wrap up
Project 2: Cows and Bulls 226
Cows and Bulls: Setting up
Designing our user interface
Filling in the table view
Setting up the game
Marking guesses
Cows and Bulls: Wrap up
Project 3: Social media 247
Social Media: Setting up
Subclassing NSSplitViewController
Creating a toolbar
Social Media: Wrap up
Project 4: Grid Browser 260
Grid Browser: Setting up
Starting with the toolbar
Layout with stack views
App Transport Security
www.hackingwithswift.com 3
Creating and destroying web views
What’s selected?
Handling navigation
Adding Touch Bar controls
Grid Browser: Wrap up
Project 5: Capital Cities 314
Capital Cities: Setting up
Creating a map view
Dropping pins
Keeping score
Capital Cities: Wrap up
Project 6: Auto Layout 340
Auto Layout: Setting up
Visual Format Language
Auto Layout anchors
Stack views
Grid views
Auto Layout: Wrap up
Project 7: Photo Memories 357
Photo Memories: Setting up
Designing with collection views
Selecting items
Loading images from disk
Importing files
Re-arranging photos
Detecting keypresses with keyUp()
So… now what?
Making a video composition
Photo Memories: Wrap up
4 www.hackingwithswift.com
Project 8: Odd One Out 408
Odd One Out: Setting up
Creating a grid of buttons
Generating layouts
Click to win
Game over
Final tweaks
Dark mode
Odd One Out: Wrap up
Project 9: GCD 444
GCD: Setting up
Performing selectors in the background
Running arbitrary code on different threads
Running delayed code
Concurrent execution
GCD: Wrap up
Project 10: WeatherBar 457
WeatherBar: Setting up
The art of nothingness
Creating a settings screen
Saving and loading settings
Parsing JSON
Updating the user interface
Creating more menu items
WeatherBar: Wrap up
Project 11: Bubble Trouble 497
Bubble Trouble: Setting up
Cleaning up the Xcode template
Bubbles… bubbles everywhere!
Setting the universe in motion
Adding some difficulty
www.hackingwithswift.com 5
Gotta pop ‘em all
Bubble Trouble: Wrap up
Project 12: Animation 523
Animation: Setting up
Constructing a test environment
Basic animator proxy use
Animating layers
Advanced layer animation
Animation: Wrap up
Project 13: Screenable 538
Screenable: Setting up
Designing the user interface
Populating the popup buttons
Drawing images and gradients
Rendering the caption
Importing a screenshot
Exporting the finished product
NSDocument and NSCoding
Screenable: Wrap up
Project 14: Shooting Gallery 589
Shooting Gallery: Setting up
Creating the game world
Creating a custom node
Click, click, bang!
Adding some polish
Shooting Gallery: Wrap up
Project 15: NSUndoManager 616
NSUndoManager: Setting up
Reversible changes
6 www.hackingwithswift.com
Doubling up methods
NSUndoManager: Wrap up
Project 16: Bookworm 628
Bookworm: Setting up
Set up for success
Manipulating arrays with NSArrayController
Binding everything
Creating an emoji formatter
Presenting a credits screen
Bookworm: Wrap up
Project 17: Match Three 654
Match Three: Setting up
A grid of balls
Matching colors
Making room for more
Tracking score
A puff of smoke
Match Three: Wrap up
Project 18: Bindings 688
Bindings: Setting up
KVC and KVO
The imperial march
Property observers and KVO
Dependent properties and formatters
Bindings: Wrap up
www.hackingwithswift.com 7