Contents
Preface 9
About this book
Notes for iOS developers
Introduction: Swift for Complete Beginners 15
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
Wrap up
Project 1: Storm Viewer 157
Storm Viewer: Setting up
Working with views
Introduction to lists
Loading our images
Responding to image selection
www.hackingwithswift.com 3
Finishing touches
Storm Viewer: Wrap up
Project 2: Cows and Bulls 189
Cows and Bulls: Setting up
Designing our user interface
Filling in the list
Marking guesses
Extra polish
Customizing the game
Now take a bow!
Cows and Bulls: Wrap up
Project 3: Views and Modifiers 230
Views and modifiers: Introduction
Why does SwiftUI use structs for views?
Why modifier order matters
Why does SwiftUI use “some View” for its view type?
Conditional modifiers
Environment modifiers
Views as properties
View composition
Custom modifiers
Custom containers
Views and modifiers: Wrap up
Project 4: Text Parser 266
Text Parser: Introduction
Bootstrapping an app
Performing semantic analysis
Finding alternative words
Detecting names and languages
Bringing in an external package
Flags and options
Text Parser: Wrap up
Project 5: MultiMap 298
MultiMap: Introduction
Creating an interactive map
Searching for locations
Handling multiple list selection
4 www.hackingwithswift.com
Switching over to searchable()
Final polish
MultiMap: Wrap up
Project 6: Animation 329
Animation: Introduction
Creating implicit animations
Customizing animations in SwiftUI
Animating bindings
Creating explicit animations
Controlling the animation stack
Animating gestures
Showing and hiding views with transitions
Building custom transitions using ViewModifier
Animation: Wrap up
Project 7: Fast Track 370
Fast Track: Setting up
From lists to grids
Downloading data from a server
Remote images and custom subviews
Playing audio
Adding some extra polish
Fast Track: Wrap up
Project 8: Odd One Out 418
Odd One Out: Setting up
Creating a grid of buttons
Generating layouts
Click to win
Game over
Odd One Out: Wrap up
Project 9: Drawing 442
Drawing: Introduction
Creating custom paths with SwiftUI
Paths vs shapes in SwiftUI
Adding strokeBorder() support with InsettableShape
Transforming shapes using CGAffineTransform and even-odd fills
Creative borders and fills using ImagePaint
Enabling high-performance Metal rendering with drawingGroup()
www.hackingwithswift.com 5
Special effects in SwiftUI: blurs, blending, and more
Animating simple shapes with animatableData
Animating complex shapes with AnimatablePair
Creating a spirograph with SwiftUI
Drawing: Wrap up
Project 10: Time Buddy 500
Time Buddy: Setting up
The art of nothing
Building the main UI
Moving, deleting, and copying
Clean ups
Time Buddy: Wrap up
Project 11: Bubble Trouble 524
Bubble Trouble: Setting up
Cleaning up the Xcode template
Bubbles… bubbles everywhere!
Setting the universe in motion
Adding some difficulty
Gotta pop ‘em all
Bubble Trouble: Wrap up
Project 12: Layout and Geometry 550
Layout and geometry: Introduction
How layout works in SwiftUI
Alignment and alignment guides
How to create a custom alignment guide
Absolute positioning for SwiftUI views
Understanding frames and coordinates inside GeometryReader
ScrollView effects using GeometryReader
Layout and geometry: Wrap up
Project 13: Screenable 594
Screenable: Setting up
Introduction to FileDocument
Custom rendering with Canvas
Loading files from your app bundle
Adding images with drag and drop
Colors and gradients
Adding shadows
6 www.hackingwithswift.com
Exporting the finished product
Screenable: Wrap up
Project 14: Shooting Gallery 664
Shooting Gallery: Setting up
Creating the game world
Creating a custom node
Click, click, bang!
Adding some polish
Shooting Gallery: Wrap up
Project 15: Accessibility 694
Accessibility: Introduction
Identifying views with useful labels
Hiding and grouping accessibility data
Reading the value of controls
Accessibility: Wrap up
Project 16: Bookworm 709
Bookworm: Setting up
Entities and attributes
Creating and querying data
Watching external objects
Adding some final touches
Bookworm: Wrap up
Project 17: Match Three 737
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: SwiftData 768
SwiftData: Introduction
Editing SwiftData model objects
Filtering @Query using Predicate
Dynamically sorting and filtering @Query with SwiftUI
www.hackingwithswift.com 7
Relationships with SwiftData, SwiftUI, and @Query
Syncing SwiftData with CloudKit
SwiftData: Wrap up
Afterword 794
8 www.hackingwithswift.com