INTRODUCTION xxiii PART I: BUILDING APPLICATIONS WITH SWIFT CHAPTER 1: A SWIFT PRIMER 3 What Is Swift? 4 Why Learn Swift? 5 Working with Constants and Variables 5 Understanding Swift Data Types 6 Using Numerical Types 6 Boolean Types 7 Using Character Types 8 Using Arrays 10 Using Dictionaries 10 Using Tuples 11 Working with Type Annotations 12 Minimizing Annotation with Type Inference 13 Clarifying Code with Type Aliasing 14 Working with Operators 15 Using Basic Operators 15 Using Compound Assignment Operators 17 Using Overfl ow Operators 17 Using Range Operators 18 Using Logical Operators 18 Using Comparison Operators 19 Using Custom Operators 19 Making Decisions with Control Flow 19 Using Conditional Statements 20 Using Loops 23 Control Transfer Statements 25 Grouping Types with Enumerations 26 Working with Functions 27 Declaring Functions 27 Specifying Parameter Names 28 Defining Default Parameters 29 Specifying Variadic Parameters 29 Specifying Constant, Variable, and In-Out Parameters 30 Function Types 31 Using Closures 32 Summary 34 CHAPTER 2: WRITING A SWIFT PROGRAM 35 Setting Up Xcode 36 Downloading from Apple''s Developer Portal 36 Downloading Xcode 36 Experimenting with Playgrounds 38 Using Playgrounds in Xcode 38 A Simple Example in Playground 38 A More Exciting Playground Example 40 Writing Swift in Xcode 41 Creating a New Swift Project 42 Creating the Swiftisms App 44 Creating the Interface 45 Creating Connections between the View Controller and the Object 46 Connecting the Buttons and the Action 50 Displaying Random Strings 51 What Next? 53 Debugging Swift Applications 53 Understanding Breakpoint Types 54 Setting Breakpoints 54 Using the Debugger 55 Using the Hierarchical View 56 Using the Debugger View and the po Command 56 Executing the Program 58 Summary 59 CHAPTER 3: CLASSES, STRUCTS, AND ENUMS 61 Working with Classes and Structs 62 Defining Classes and Structs 62 Initializing Classes and Structs 64 Creating an Initial Value 64 Defining Your Own Initializer 65 Writing an Initializer 66 Working with Multiple Initializers 68 Deinitializing Classes 69 Working with Properties 70 Using Simple Properties 70 Using Lazy Properties 72 Using Computed Properties 72 Observing Property Changes 73 Understanding Methods 74 Understanding the Difference between Classes and Structs 76 Working with Enumerations 78 Summary 79 CHAPTER 4: CONCURRENCY IN SWIFT 81 What Is Concurrency? 82 Understanding Processes 82 Understanding Threads 84 Why Concurrency? 85 Designing Concurrent Programs 85 Concurrency in iOS and OS X 86 Understanding Event Loops 86 Understanding Run Loops 87 Understanding Asynchronous Methods 87 Understanding NSTimer 89 Understanding NSThread 91 Understanding Grand Central Dispatch 92 Summary 94 CHAPTER 5: INTERFACING WITH WEB SERVICES 95 Understanding Web Services 95 Implementing a Web Services Client 97 Signing Up for a Firebase Account 97 Laying Out the User Interface 98 Hooking Up the Add Album View Controller 101 Adding the Album Creation View Controller 103 Hooking Up the Segue 104 Creating the Album Model 104 Communicating with Firebase 106 Creating the Firebase Connector 106 Making Asynchronous Web Requests 109 Processing JSON Data 110 Creating New Albums 111 Deleting Albums 112 Wiring Up the View and Data Models 112 Wiring Up the Segue 115 Run the Application 116 Summary 118 CHAPTER 6: STORING DATA WITH CORE DATA 119 What Is Core Data? 120 The Core Data Stack 120 Using Core Data with Swift 121 Setting Up the Birthdays Application 122 Getting User Input 128 Examining Xcode Files 129 Adding Methods to the AppDelegate Class 134 Handling the Dialog Sheet 135 Hooking Up the Dialog Sheet 137 Displaying Data 137 Implementing the Table View''s Behavior 138 The Completed Application Delegate 140 Summary 145 PART II: ADVANCED SWIFT CONCEPTS CHAPTER 7: EXTENDING CLASSES 149 Working with Class Extensions 150 Adding Methods to Classes 150 Adding Properties to Classes 152 Mutating Classes in Extensions 153 Specifying Behavior with Protocols 153 Adopting Protocols 154 Declaring Properties and Methods 154 Working with Optional Methods and Properties 156 Protocols Are Types 158 Checking for Protocol Conformance 158 Using Protocol Types in Collections 159 Composing Protocols 160 Inheriting from Other Protocols 160 Adopting Protocols in Class Extensions 161 Working with Generics 163 Generic Classes 164 Working with Type Constraints 165 Summary 166 CHAPTER 8: ADVANCED DATA TYPES 167 Working with Enums and Algebraic Data Types 168 Matching Patterns 168 Putting It All Together with JSON 170 Working with Optional Types 172 Working with Null Values 172 Excluding Null Values 173 Understanding Swift''s Version of Null 173 Chaining Optional Values 174 Understanding Type Casting 175 Grouping Values with Tuples 177 Custom Operators 179 Defining Custom Operators 183 Defining Precedence and Associativity 183 A Final Word about Operators 186 Using Functions and Closures 187 Summary 188 CHAPTER 9: BRIDGING SWIFT AND OBJECTIVEÂ]C 189 The Successor to Objective-C 190 Introducing Namespaces and Modules 191 Organizing Code with Namespaces 191 Distributing Code with Modules 193 Using Access Modifi ers 193 Specifying an Access Level 194 How Swift and Objective-C Interact 194 Using Swift Classes in Objective-C 195 Generating an Objective-C Header 195 Objective-C and Swift-Only Features 196 Using Swift Code in an Objective-C Application 196 Using Objective-C in Swift 198 Generating a Bridging Header 198 Calling Objective-C Code from Swift 199 Using C and C++ Code with Swift 202 Working with C Scalar Types 202 Accessing Memory with C Pointers 203 Working with Global Constants and Defines and Swift 204 Calling C Code from Swift 204 Using C++ Code in Swift 206 Summary 206 CHAPTER 10: DEBUGGING SWIFT APPLICATIONS 207 The Art of Debugging 208 Creating CircleView 208 Drawing the Circle 209 Adding Sliders 210 Completing the Project 211 Printing Values 213 Working with Debuggers 215 Examining Errors with lldb, the LLVM Debugger 216 Basic Breakpoints 216 Using lldb to Inspect Programs 218 Using the Variable Pane 218 Entering Commands in the lldb Command Prompt Pane 218 Displaying Code Hierarchy with the lldb Ribbon 219 Walking through a Program with the lldb Ribbon 221 Removing or Disabling Breakpoints 222 Setting Conditional Breakpoints 222 Setting Symbolic Breakpoints 223 Exploring the Debugger 225 Summary 228 CHAPTER 11: THE SWIFT RUNTIME 229 What Is a Runtime? 230 Understanding the ObjectiveÂ]C Runtime 231 Dispatching Methods Dynamically in ObjectiveÂ]C 231 Message Passing 233 Resolving Methods 233 Messages and Methods 237 Exploring the Swift Runtime 238 Understanding Virtual Method Tables 239 Loading Swift Programs 241 Summary 245 APPENDIX: AN OVERVIEW OF C 247 Comparing Procedural and Object-Oriented Programming Styles 248 Understanding the Importance of C Language Syntax 249 Defining Data with Variables and Arrays 249 Integral Data Types 249 Floating-Point Data Types 251 Arrays 252 Typedefs 253 Enums 254 Performing Calculations with Operators 254 Arithmetic Operators 254 Logical Operators 256 Relational Operators 258 Bitwise Operators 258 Bitshift Operators 259 Referencing Data with Pointers 260 Organizing Data with Structs 261 Generalizing Data with Unions 261 Referencing Functions with Function Pointers 262 INDEX 265.
Professional Swift