Synopses & Reviews
Get started fast with Swift programming for iOS and OS X
Learning Swift Programming is a fast-paced, hands-on introduction to writing production-quality iOS and OS X apps with Apple’s new programming language. Written for developers with previous experience in any other modern language, this book explains Swift simply and clearly, using relevant examples that solve realistic problems.
Author Jacob Schatz’s popular Skip Wilson video tutorials on YouTube have already helped thousands of Apple developers get started with Swift. Now, he helps you take full advantage of Swift’s advanced design, remarkable performance, and streamlined development techniques.
Step-by-step, you’ll move from basic syntax through advanced features such as closures and generics–discovering helpful tips and tricks along the way. After you’ve mastered Swift’s building blocks and learned about its key innovations, a full section of case studies walks you through building complete apps from scratch.
- Compare Swift with Objective-C, JavaScript, Python, Ruby, and C
- Collect data with arrays and dictionaries, and store it with variables and constants
- Group commonly-used code into functions for easy reuse
- Structure your code with enums, structs, and classes
- Use generics to get more done with less code
- Write closures to share small blocks of functionality
- Use optionals to write more robust, crash-resistant, and cleaner code
- Integrate existing Objective-C code into new Swift apps
- Program on the bit and byte level with advanced operators
- Implement efficient design patterns with protocols and delegates
- Create animated 2D games with SpriteKit, and 3D games with SceneKit
Contents at a Glance
1 Getting Your Feet Wet
Building Blocks of Swift
Optionals: A Gift to Unwrap
Tuples
Number Types and Converting Between Them
Coming to Swift from Objective-C and C
2 Collecting Data
Using Arrays
Modifying Arrays
Using Dictionaries
3 Making Things Happen: Functions
Defining Functions
More on Parameters
4 Structuring Code: Enums, Structs, and Classes
Enums
Structs
5 Making a Game
Building a User Interface (UI)
The Action-Packed View Controller
6 Reusable Code: Closures
What Are Closures?
Closures in Other Languages
How Closures Work and Why They’re Awesome
7 Subscripts and Advanced Operators
Writing Your First Subscript
Bits and Bytes with Advanced Operators
Customizing Operators
Making Your Own Operators
Bits and Bytes in Real Life
8 Protocols
Writing Your First Protocol
Animizable and Humanizable
Delegation
Protocols as Types
Protocols in Collections
Optional Chaining
9 Becoming Flexible with Generics
The Problem That Generics Solve
10 Games with SpriteKit
Setting Up the Project
The Start Screen
Dangerous Ground
A Hero to the Rescue
Enemies in Motion
Spawned Obstacles
Smashing Physics
11 Games with SceneKit
Creating DAE Files
Creating a New SceneKit Project
Your SceneKit Files
Making the Game
Bridging the Gap to Objective-C
12 Apps with UIKit
Application Types
Loading a Table View
Loading Data from a URL
Synopsis
Learning Swift Programming is a fast-paced, hands-on introduction to developing iOS and OS X apps with Apple's hot new programming language. This book is designed to provide an experienced programmer with a complete understanding of the Swift language and how to leverage it to build professional, well-designed iOS and OS X apps. Author Jacob Schatz – creator of immensely popular Swift programming video tutorials on YouTube – explains Swift in simple, understandable terms. Schatz explains Swift with pertinent examples that make the material exciting and that show the user how to solve common problems.
About the Author
Jacob Schatz is a senior software engineer with more than eight years of experience writing code for the masses. His popular Skip Wilson YouTube channel has helped thousands of people around the world learn programming languages like Swift and Python. Though lately he has been deep into Swift, he also writes volumes of code in JavaScript, Python, Objective-C, and other languages. Jacob is always selectively consuming the latest programming trends. He has a passion for making a difference and is constantly solving problems. He is, at heart, an educator, and enjoys teaching and finding new ways to explain advanced concepts.
Table of Contents
Introduction 1
1 Getting Your Feet Wet 5
Building Blocks of Swift 5
Using Comments 6
Inference Everywhere! 6
Optionals: A Gift to Unwrap 8
Printing Your Results 11
Implicitly Unwrapped Optionals 11
Tuples 11
Number Types and Converting Between Them 12
Coming to Swift from Objective-C and C 14
Control Flow: Making Choices 14
Switching It Up: switch Statements 21
Stop...Hammer Time 23
Summary 25
2 Collecting Data 27
Using Arrays 27
Your First Array 27
A Quicker Array 28
Using AnyObject 29
What’s an Array Made Of? 29
Differences Between NSArrays and Swift Arrays 30
Modifying Arrays 30
Accessing Array Elements 30
Adding Elements to an Array 31
Removing Elements from Arrays 31
Iterating Over Arrays 32
Extra Bits of Arrays 32
Emptying an Array 33
Using Dictionaries 33
Adding, Removing, and Inserting with Dictionaries 34
Iterating Over Dictionaries 34
Extra Bits of Dictionaries 35
Emptying a Dictionary 35
Testing Dictionaries for the Presence of Values 35
Putting It All Together 36
Summary 38
3 Making Things Happen: Functions 39
Defining Functions 40
Return Types 41
Multiple Return Values 42
More on Parameters 43
External Parameter Names 43
Default Parameter Values 44
Variadic Parameters 45
In-Out Parameters 46
Functions as Types 47
Putting It All Together 48
Summary 52
4 Structuring Code: Enums, Structs, and Classes 53
Enums 54
Which Member Was Set? 55
Associated Values 55
Raw Values 56
Structs 58
Defining Methods in Structs 59
Structs Are Always Copied 60
Mutating Methods 61
Classes 62
Initialization 62
What Is a Reference Type? 64
Do I Use a Struct or a Class? 64
Forgot Your Pointer Syntax? 65
Property Observers 65
Methods in Classes 66
Summary 70
5 Making a Game 71
Building a User Interface (UI) 71
Importing Assets 72
Adding Buttons and Labels 73
The Action-Packed View Controller 75
Displaying the Player’s Moves 77
Developing the Concept for an AI 78
Managing the Game State 79
Implementing the AI Logic 81
Finding Victory Conditions 83
Moving In for the Win 85
Defending the AI’s Turf and Stealing Center Stage 88
Taking Sides 89
When It’s a Draw 89
Summary 91
6 Reusable Code: Closures 93
What Are Closures? 93
Closures in Other Languages 94
How Closures Work and Why They’re Awesome 95
The Closure Syntax 96
Inferring Using Context 96
Arguments Have a Shorthand, Too 97
Sorting a Custom Car Class 97
Closures Are Reference Types 98
Automatic Reference Counting 99
Strong Reference Cycles 100
Trailing Closures 106
Summary 107
7 Subscripts and Advanced Operators 109
Writing Your First Subscript 110
Bits and Bytes with Advanced Operators 113
Bitwise NOT 114
Bitwise AND 115
Bitwise OR 116
Bitwise XOR 117
Shifting Bits 118
UInt8, UInt16, UInt32, Int8, Int16, Int32, and So On 119
Value Overflow and Underflow 119
Customizing Operators 120
Making Your Own Operators 122
Bits and Bytes in Real Life 123
Summary 127
8 Protocols 129
Writing Your First Protocol 129
Properties 131
Animizable and Humanizable 134
Methods 135
Delegation 136
Protocols as Types 138
Protocols in Collections 139
Protocol Inheritance 140
Protocol Composition 141
Protocol Conformity 143
Optional Protocol Prerequisites 145
Optional Chaining 147
Back to Optional Protocol Requisites 148
Useful Built-in Swift Protocols 149
Summary 152
9 Becoming Flexible with Generics 153
The Problem That Generics Solve 153
Other Uses for Generics 155
Generics for Protocols 158
The where Clause 159
Summary 162
10 Games with SpriteKit 163
Setting Up the Project 164
Loading a Scene View 165
Importing Image Assets 166
The Start Screen 166
Custom Colors 167
Handling Taps 168
Dangerous Ground 168
Positioning Obstacles 169
Moving Obstacles 171
A Hero to the Rescue 172
Rotating Nodes 173
Bar Hopping 174
Enemies in Motion 175
Elements of Chance 176
Tracking a Brick 176
Tracking All Blocks 177
Spawned Obstacles 178
Generating Barriers 178
Hardcore Mode 179
Smashing Physics 179
Invisible Cages 180
A Hero’s Death 181
Summary 182
11 Games with SceneKit 183
Creating DAE Files 183
Creating a New SceneKit Project 185
Your SceneKit Files 186
Setting the Stage 187
Importing the Assets 188
Material Properties 189
Making the Game 191
Adding a Floor 193
Adding the Level 194
Placing Each Block 195
Creating a Hero 196
Bridging the Gap to Objective-C 196
Back to the Hero 197
Adding the Controls 198
Summary 201
12 Apps with UIKit 203
Application Types 203
Single-View Applications 204
Adding Constraints 209
Adding a New View 209
Loading a Table View 213
Loading Data from a URL 217
Summary 218
TOC, 9780133950403, MF