Topic: Mobile

Beginning Swift Programming (1119042887) cover image

Beginning Swift Programming

Wei-Meng Lee
ISBN: 978-1-119-04288-4
E-book
288 pages
December 2014
Other Available Formats: Paperback
Electronic Version: US $26.99 Purchase This E-book

About This Title  |  Errata  |  P2P Forum for This Title
Table of Contents

INTRODUCTION xxiii

CHAPTER 1: INTRODUCTION TO SWIFT 1

What Is Swift? 2

Why Swift Is Important 3

Setting Up the Environment 3

Creating a Playground Project 4

Creating an iOS Project 6

Swift Syntax 10

Constants 10

Variables 12

String Interpolation: Including Values in Strings 14

Statements 14

Printing 15

Comments 15

Summary 17

CHAPTER 2: DATA TYPES 19

Basic Data Types 20

Integers 20

Types of Integers 21

Integer Operations 22

Integer Literals 22

Floating?]Point Numbers 23

Floating?]Point Operations 23

Floating?]Point Literals 24

Type Alias 25

Boolean 25

Tuples 26

Optional Types 27

Implicitly Unwrapped Optionals 28

Optional Binding 29

Unwrapping Optionals Using “?” 30

Enumerations 30

Using Enumeration in Switch Statements 31

Enumeration Raw Values 32

Auto?]Increment for Raw Values 33

Associated Values 34

Enumeration Functions 35

Summary 36

CHAPTER 3: STRINGS AND CHARACTERS 39

Strings 40

Mutability of Strings 40

Strings as Value Types 40

Characters 41

Concatenating Strings 43

Special Characters 44

Unicode 45

Common String Functions 46

Equality 46

Prefi x and Suffi x 47

Length 47

Substrings 48

Converting Strings to Arrays 51

Type Conversion 51

Interoperability with NSString 52

Casting String to NSString 53

Using NSString Directly 54

String or NSString? 55

Summary 56

CHAPTER 4: BASIC OPERATORS 59

Assignment Operator 60

Arithmetic Operators 61

Addition Operator 61

Subtraction Operator 62

Multiplication Operator 62

Division Operator 63

Modulus Operator 63

Increment and Decrement Operator 63

Compound Assignment Operators 65

Nil Coalescing Operator 65

Comparison Operators 66

Equal To and Not Equal To 66

Greater Than or Equal To 67

Less Than or Equal To 67

Range Operators 68

Logical Operators 69

NOT 69

AND 70

OR 71

Combining Logical Operators 71

Ternary Conditional Operator 72

Summary 73

CHAPTER 5: FUNCTIONS 75

Defi ning and Calling a Function 76

Input Parameters 76

Returning a Value 76

Returning Multiple Values 77

Function Parameter Names 77

External Parameter Names Shorthand 79

Default Parameter Values 79

Variadic (Variable) Parameters 80

Constant and Variable Parameters 81

In?]Out Parameters 82

Function Types 83

Defining a Function Type Variable 83

Calling a Function Type Variable 84

Returning Function Type in a Function 85

Nested Functions 85

Summary 86

CHAPTER 6: COLLECTIONS 89

Arrays 90

Mutability of Arrays 90

Array Data Types 90

Retrieving Elements from an Array 91

Inserting Elements into an Array 91

Modifying Elements in an Array 92

Appending Elements to an Array 92

Checking the Size of an Array 93

Removing Elements from an Array 93

Iterating over an Array 93

Creating an Empty Array 94

Testing Arrays for Equality 95

Dictionaries 96

Mutability of Dictionaries 97

Retrieving Elements from a Dictionary 97

Checking the Size of a Dictionary 98

Modifying an Item in the Dictionary 98

Removing an Item from the Dictionary 99

Iterating over a Dictionary 99

Creating an Empty Dictionary 101

Testing Dictionaries for Equality 101

Copying the Behavior of Arrays and Dictionaries 102

Summary 103

CHAPTER 7: CONTROL FLOW AND LOOPING 107

Flow Control 108

If Statement 108

If?]Else Statement 109

Switch Statement 110

Matching Numbers 111

Matching Characters 112

Fallthrough 112

Matching a Range of Numbers 113

Matching Tuples 114

Value Bindings 115

Where Clause 117

Looping 118

For?]In Loop 118

Traditional For Loop 121

While Loop 122

Do?]While Loop 123

Control Transfer Statements 124

Break Statement 124

Continue Statement 126

Labeled Statement 126

Summary 128

CHAPTER 8: STRUCTURES AND CLASSES 131

Structures 132

Memberwise Initializers 132

Structures as Value Types 133

Comparing Structures 135

Classes 135

Defining a Class 136

Properties 136

Stored Properties 136

Lazy Stored Properties 137

Computed Properties 138

Motivation Behind Computed Properties 139

The newValue keyword 140

Read?]Only Computed Properties 141

Property Observers 141

Typed Properties 143

Initializers 144

Initializers and External Parameter Names 145

Initializing Variables and Constants During Initialization 147

Classes as Reference Types 147

Comparing Instances—Identity Operators 149

Comparing Instances—Equivalence Operators 150

Methods in Classes 151

Instance Methods 151

Local and External Parameter Names for Methods 152

The self Property 154

Type Methods 155

Methods in Structures 155

Summary 157

CHAPTER 9: INHERITANCE 161

Understanding Inheritance 162

Defi ning a Base Class 162

Instantiating a Base Class 162

Creating an Abstract Class 163

Inheriting from a Base Class 164

Overriding Initializers 164

Overloading Initializers 165

Creating Abstract Methods 167

Overloading Methods 169

Preventing Subclassing 170

Types of Initializers 171

Default Initializer 171

Designated Initializers 172

Convenience Initializers and Initializer Chaining 174

Calling Initializers in Subclasses 176

Extensions 177

Extending Methods 177

Extending Properties 177

Access Controls 178

Internal 179

Private 180

Public 181

Summary 181

CHAPTER 10: CLOSURES 185

Understanding Closures 186

Functions as Closures 186

Assigning Closures to Variables 187

Writing Closures Inline 188

Type Inference 188

Shorthand Argument Names 189

Operator Function 190

Trailing Closures 190

Using the Array’s Three Closure Functions 190

The map Function 191

Example 1 191

Example 2 192

The filter Function 192

Example 1 192

Example 2 193

The reduce Function 194

Example 1 194

Example 2 195

Using Closures in Your Functions 196

Summary 198

CHAPTER 11: PROTOCOLS AND DELEGATES 201

Understanding Protocols 202

Defining and Using a Protocol 202

Conforming to a Protocol 202

Optional Methods 204

Conforming to Multiple Protocols 206

Property Requirements 206

Initializer Requirements 207

Understanding Delegates 207

Delegates as Event Handlers 208

A Practical Example of Protocols and Delegates 211

Summary 213

CHAPTER 12: GENERICS 217

Understanding Generics 218

Using Generic Functions 218

Multiple Type Parameters 219

Specifying Type Constraint 220

Generic Types 221

Generic Classes 221

Generic Structures 223

Generic Type Extension 224

Using Generics in Protocols 225

Specifying Requirements for Associated Types 228

Summary 229

APPENDIX: EXERCISE ANSWERS 233

INDEX 247