A B C D E F G H I J-K L M N O P Q R S T U V W X-Y-Z

Symbols

1> prompt, 8

+ (addition) operator, 29

= (assignment) operator, 14, 30, 44, 246

* (asterisk), 22, 379

: (colon), 16

/* ... */ (comment blocks), 7

-= compound assignment operator, 31

*= compound assignment operator, 31

/= compound assignment operator, 31

%= compound assignment operator, 31

+= compound assignment operator, 31

+ (concatenation) operator, 9

{} (curly braces), 109, 279, 302

-- (decrement) operator, 28

/ (division) operator, 29

// (double forward slash), 7

== (equality) operator, 31

! (exclamation point), 208

> (greater than) operator, 31

>= (greater than or equal to) operator, 31

++ (increment) operator, 28

!= (inequality) comparison operator, 31

== (is-equal) operator, 415

< (less than) operator, 31

<= (less than or equal to) operator, 31

&& (logical AND) operator, 33-34

! (logical NOT) operator, 35

|| (logical OR) operator, 34-35

- (minus sign), 379

* (multiplication) operator, 29

?? (nil coalescing) operator, 83

? (optional chaining) operator, 318

() parentheses, 64

+ (plus sign), 379

? (question mark), 78, 208

% (remainder) operator, 29-30

-> ReturnType, 109-110

; (semicolon), 22

- (subtraction) operator, 29

- (unary minus) operator, 29

+ (unary plus) operator, 29

_ (underscore), 68, 206, 383

! (unwrap) operator, 80-81

_ (wildcard underscore), 101

A

[top]

Abrahams, Dave, 343

access control

in extensions, 313

internal access, 248

private access, 248

public access, 247-248

types, 247-248, 258

accessing

arrays, 41-42

methods

instance methods on enums, 192

with optional chaining, 322-324

REPL (Read-Eval-Print-Loop), 8

super, 172-174

type properties, 239-241

accessors

inheriting, 231-233

property accessors

getters, 225-226

inheriting, 231-233

overriding, 231-233

overview, 225

setters, 226-228

addition-assignment compound operator, 43

addition (+) operator, 29

adoption of protocols

definition of, 279

multiple protocols, 285-290

syntax, 280-281

aliasing (type), 246-247

allocation, Objective-C, 383-384

AND operator, 33-34

annotation, 16

Any keyword, 256-257

AnyObject keyword, 256-257

appending array elements, 43

append() method, 43

Apple documentation, 417

App Store, downloading Xcode from, 2

arc4random() function, 66

ARC (Automatic Reference Counting), 262

closures, 272-275

how it works, 265

reference cycles

identifying, 266-269

resolving, 269-272

reference relationships, 265-266

strong references, 265

unowned references, 270-272, 276

weak references, 269-270

area() method, 175

areIntsEqual() function, 329

areStringsEqual() function, 329

areTheseEqual() function, 329-330

arguments, 114

validation, 112

values, changing, 120-121

arithmetic operators, 29

arrays, 332

accessing values in, 41-42

appending elements to, 43

compared to dictionaries, 56

creating, 41-42

declaring, 39-41

for-in loops, 98-99

for loops, 96

functions as members of arrays, 133

generic function to return an array, 331

indexes, 41-42

inserting elements into, 43-44

member types, 40

methods, 46

properties, 46

removing items from, 45-46

replacing items in, 44-45

subscripts, 41-42

tuples, 55-56

type bridging in Objective-C, 386

updating items in, 44-45

zero-based arrays, 41

as keyword, 255-256

as! keyword, 291

as? operator, 291

assignment operator, 14, 30, 44

assignment operator (=), 246

Assistant Editor, 6

associated types, 337-339

associated values

bindings, 191

enums, 189-190

named parameters, 190

asterisk (*), 22, 379

AudioCD struct, 284-285

Automatic Reference Counting. See ARC (Automatic Reference Counting)

B

[top]

base classes, 166, 170

binary operators

assignment operator, 14, 30, 44

combinations of logical operators, 35

comparison operators, 31-32

compound assignment operators, 30-31

explained, 29

logical AND operator, 33-34

logical NOT operator, 35

logical OR operator, 34-35

range operators, 32-33, 36

remainder operator, 29-30

standard arithmetic operators, 29

bindings, associated values, 191

blocks, do-catch, 367-369

BluePotion struct, 294

Book class, 287

Bool data type, 20

Boolean types, 20

Bool type (Objective-C), 399

break keyword, 65, 71, 103

bridging

definition of term, 384

module bridging, 384-385

type bridging, 385-387

Array and NSArray, 386

Dictionary and NSDictionary, 387

Numeric Types and NSNumber, 386

String and NSString, 386

bridging header, 384

Bundle Identifier, 4

ButtonDelegate class, 293

ButtonDelegateProtocol, 293

buttonTapped() method, 293

C

[top]

calling

functions, 108

methods, 322-324

capital letters as generic placeholder types, 136

capture, closure capture lists, 272-275

casting down. See unwrapping optionals

catching errors, 366-371

from throwing function, 367-369

trying without catching, 369-371

chaining

chaining functions together

explained, 410-411

flatMap functions, 412-413

initializer chaining, 211-215

optional chaining

definition of term, 315-316

importance of, 325

methods, 322-325

properties, 316-318

subscripts, 318-322

use cases, 316

when to use, 325

checking for protocol conformance, 291

child classes, 167

Circle class, 156

classes, 148. See also inheritance; methods

base classes, 166, 170

Book, 287

ButtonDelegate, 293

child classes, 167

Circle class, 156

class identity, 176-177

comparing to structs, 149-150, 155-156

class instance references, 159-160

differences, 157-158

instance equality, 160

instance methods, 151-154

mutating properties, 158

properties, 150-151

creating, 389-393

deinitializing, 261-265, 276

DetailVC, 392

Document, 320

DVD, 287

Employee, 271-272

GameManager, 296

initialization, 207-209, 262-263

default initializer parameters, 207-208

failable initializers, 208-209

initialization delegation, 211-212

Library, 320

MathFunction, 273

Media, 286

NSArray, 79, 386

NSDictionary, 47, 79, 387

NSError, 364

NSMutableDictionary, 47

NSNumber, 386

NSString, 386

Objective-C classes

exposing to Swift, 394

extending with Swift, 394-395

parent classes, 167

Person, 267

Phone, 266, 269-270

properties, 155-156

Rectangle, 214

Site, 319

SomeClassWithExpensiveOperation, 223

SomeClassWithLazyVar, 223

Square, 215, 232

StackOfPapers, 250, 251

subclasses, 167

creating, 167-168

overriding instance methods, 170-171

superclasses, 167

overriding inherited methods, 169

syntax, 155

TestClass, 263

TheLegendOfZelda, 295

updating in storyboard, 395-396

when to use, 160-161

class keyword, 239

class subscripts, 166

clauses

else

else-if syntax, 61-63

if-else syntax, 60-63

where, 87

closed for modification, 301

closed-range operator, 32, 36

closing REPL (Read-Eval-Print-Loop), 9

closure capture lists, 272-275

closures, 134

as parameters, 137

shorthand argument names, 138

single-expression closures, 137

strong reference cycles, 272-275

structure of, 134-137

trailing closures, 139, 142-143

Cocoa error handling, 362-363

collections

arrays

accessing values in, 41-42

appending elements to, 43

compared to dictionaries, 56

creating, 41-42

declaring, 39-41

indexes, 41-42

inserting elements into, 43-44

member types, 40

methods, 46

properties, 46

removing items from, 45-46

replacing items in, 44-45

subscripts, 41-42

tuples, 55-56

updating items in, 44-45

zero-based arrays, 41

definition of term, 39

dictionaries

adding data to, 49-50

compared to arrays, 56

explained, 47

initializing, 48

key-value pairs, 47-48

methods, 50

properties, 50

removing items from, 50

tuples, 55-56

type inference, 48-49

heterogeneous collections, 353-355

homogeneous collections, 353-355

sets

adding elements to, 51-52

creating, 51

definition of term, 51

performing actions on, 54

removing elements from, 52-53

retrieving elements from, 52

use case, 56

when to use, 53-55

tuples, 55-56, 68-69

CollectionType protocol extension, 345

colon (:), 16

combining

logical operators, 35

strings, 9

comments, 7

Comparable protocol, 415, 419

comparison operators, 31-32

comparisons

comparison operators, 31-32

structs and classes, 149-150, 155-156

comparing class instance references, 159-160

differences, 157-158

instance equality, 160

instance methods, 151-154

mutating properties, 158

properties, 150-151

values, 92-93

composition, protocol composition, 289-290

compound assignment operators, 30-31, 43

computed properties, 186, 224-225

computed instance properties, 303-305

computed type properties, 241-242

concatenating strings, 21

concatenation (+) operator, 9

concrete types, converting protocol sequences to, 355-357

conditional checking, validating user input with, 85-86

conditional operators, 35

conditionals

advanced pattern matching, 72-73

if statement, 59-64

C/Objective C versus Swift, 60

else-if syntax, 61-63

if-else, 60-63

ternary conditional operators, 63-64

overview, 59

switch statement

break statement, 71

explicit break, 65

fall through, 65

fallthrough statement, 71-72

matching multiple values, 65-66

range matching, 66-68

syntax, 64-65

tuple matching, 68-69

where keyword, 69-70

ternary conditional operators, 63-64

conditions

for-condition-increment loop, 94

while loops, 90

configureView() method, 393, 395

conformance (protocol)

checking for, 291

definition of term, 279

constants, 149

assigning values to, 14

creating, 7, 79

declaring, 14-15, 22-23

explained, 13-14

naming, 15

when to use, 14

constraints, type, 329-332

continue statements, 102-103

controlling program flow. See program flow, controlling with conditionals

control of execution, transferring

break statement, 71

fallthrough statement, 71-72

convenience initializers, 211

convenience keyword, 211, 308

converting

between integers and floating-point numbers, 19

protocol sequences, 355-357

copying value types, 149

Core Data, 302

counts, reference count, 262

count property, 46

Create a New Xcode Project command, 2

creating. See also declaring; initialization

arrays, 41-42

classes, 389-393

constants, 7, 79

generic types, 333-336

immutable variables, 7

playgrounds, 6

protocol extensions, 346-349

protocols, 280-281

queues, 334-336

reference cycles, 266-269

sets, 51

subscripts, 249-252

variables, 79

Xcode projects, 2-6

curly braces ({}), 109, 279, 302

custom initializers

adding with extensions, 308-309

enums, 203-204

structs, 203

customizing error types, 364-365

custom operators, 416-419

D

[top]

data source delegates, 292

data types. See types

deallocation, 261

declaring. See also creating; initialization

arrays, 39-41

constants, 14-15, 22-23

enums, 185

functions, 111

methods

in protocols, 282-284

type methods, 242-246

mutating functions, 283

stored properties, 221-222

variables, 14-15, 22-23

decrement operator (--), 28

default initializer parameters, 207-208

default initializers, 201

default protocol implementations, overriding, 349-352

default values

default parameter values, 120

default property values, 200

setting, 202-206

defer keyword, 122

deferring execution, 122-123, 371-374

defer statement, 122, 371-374

defining. See declaring

definition of Swift, 1-2

deinitializers, 276

deinitializing classes, 261-265, 276

deinit keyword, 262

delegates

data source delegates, 292

delegate execution, deferring, 372-373

protocols and, 292-297

delegation

initialization delegation

classes, 211-212

definition of term, 209

structures, 210-211

protocols, 292-297

deleting projects, 8

Dependency Inversion Principle, 292

dequeue function, 335

dequeueing, 334-335

description() method, 177

designated initializers, 211, 218

DetailVC class, 392

dictionaries

adding data to, 49-50

compared to arrays, 56

explained, 47

for-in loops, 100

initializing, 48

key-value pairs, 47-48

methods, 50

properties, 50

removing items from, 50

sorting, 100

tuples, 55-56

type inference, 48-49

Dictionary type, 332, 387

didSet keyword, 228, 233, 236

division (/) operator, 29

do-catch blocks, 367-369

Document class, 320

Double data type, 18-19

double forward slash (//), 7

do-while loops, 90-94

downcasting, 255-256

downloading

starter project (Objective-C), 387-389

Xcode, 2

DVD class, 287

DVD struct, 284-285

dynamic dispatch, 349

E

[top]

Either, 191

elements

array elements

appending, 43

inserting, 43-44

member types, 40

removing, 45-46

replacing, 44-45

updating, 44-45

set elements

adding, 51-52

removing, 52-53

retrieving, 52

else clause

else-if syntax, 61-63

if-else syntax, 60-63

Emoji, 15

Employee class, 271-272

Enemy struct, 294

enqueue function, 335

enqueueing, 334-335

enumerators. See enums

enums, 47, 183-184, 204-206

associated values, 189-190

creating, 185-188, 192-194

custom initializers, 203-204

initialization, 204-206

raw values, 185-186

getting, 186

setting, 186-187

structure of, 184-185

switching values, 190-192

syntax, 187-189

equality

equality (==) operator, 31

instance equality, 160

equality (==) comparison operator, 31

Equatable protocol, 415

error handling

Cocoa error handling, 362-363

explained, 361

out of bounds errors, 43

Swift error handling

catching errors, 366-371

customizing error types, 364-365

deferring execution, 371-374

ErrorType protocol, 364-365

overview, 363-364

throwing errors, 365-366

errors. See error handling

ErrorType protocol, 364-365, 375

exclamation point (!), 208

exclusiveOr() method, 54

execution, deferring, 122-123, 371-374

exhaustive, 64

exiting functions, 121-122

explicitly declaring data types, 24

explicitly unwrapped optionals. See optional values

explicit type bridging, 385

exposing Objective-C classes to Swift, 394

extending

generic types, 336-337

Objective-C classes with Swift, 394-395

extension keyword, 302

extensions, 301

access control in, 313

adding functionality with, 303

computed instance and type properties, 303-305

custom initializers, 308-309

instance and type methods, 305-306

nested types, 309-310

protocol adoption and conformance, 310-312

subscripts, 306-307

advantages of, 313

availability to subclasses, 313

Core Data, 302

explained, 301-302

Open/Closed Principle, 302

protocol extensions, 280

CollectionType, 345

creating, 346-349

explained, 344-345

type constraints, 352-353

SequenceType, 405

structure of, 302-303

external parameter names, 118-120

F

[top]

factory methods, 242

failable initializers, 208-209

fall through, 65

fallthrough statement, 71-72

FIFO (First-In-First-Out), 334

files

viewing, 6

Objective-C file structure

explained, 378

header files, 378-380

implementation files, 380-382

filter function, 409-410

filtering values

filter function, 409-410

for-in loops, 101-102

final keyword, 174, 231

finding

minimum/maximum ranges of integers, 17

properties/methods of data types, 20-21

first-class types, 184

First-In-First-Out (FIFO), 334

first property, 46

flatMap function, 411-414

chaining together, 412-413

on optionals, 412-414

on sequences, 411

flattening objects

flatMap on optionals, 412-414

flatMap on sequences, 411

floating-point numbers, converting, 19

folders, ObjCInteropTests, 399

forced unwrapping, 80

force try statement (try!), 369-371, 375

forEach function, 143, 406-409, 419

for loops, 94

arrays, 96

C, 95

for-condition-increment loops, 94-95

for-in loops, 96-101

arrays, 98-99

data types, iterating, 97-100

dictionaries, 100

filtering, 101-102

items, 96

pattern matching, 101-102

ranges, 97

strings, 97-98

tuples, 101

Swift, 95

funcName, 109

functional programming, 127, 403-404. See also functions

functions, 107. See also methods

arc4random(), 66

areIntsEqual(), 329

areStringsEqual(), 329

areTheseEqual(), 329-330

calling, 108

chaining functions together, 410-411

declaring, 111

default parameter values, 120

deferring execution, 122-123

dequeue, 335

dynamic dispatch, 349

enqueue, 335

exiting early, 121-122

external parameter names, 118-120

filter, 409-410

flatMap, 411-414

chaining together, 412-413

on optionals, 412-414

on sequences, 411

forEach, 143, 406-409, 419

generic functions

comparing equality with, 329

generic function to return an array, 331

structure of, 328

higher order functions, 127

closures, 134

function parameters, 132-134

nested functions, 130-132

returning, 128-130

in-out parameters, 120-121

instance methods, 135

internal parameter names, 119

length of, 108

map, 404-408, 419

median function, rewriting, 139-140

mutating functions, 283

operator functions, 139

overriding, 357

parameters, 111-112, 132-134

performing as members of arrays, 133

print(), 329

printWeather(), 328

reduce function, 136

return types and

functions with no parameters and no return type, 110

functions with return types, 114-118

returning mean, median, mode, 116-117

static dispatch, 349

structure of, 108

styMap, 345

syntax, 109

throwing functions, 367-369

types, 110-111

unique, 331

variadic parameters, 113-114

G

[top]

GameManager class, 296

GameManagerDelegate protocol, 295

generics

associated types, 337-339

explained, 327-328

functions

comparing equality with, 329

generic function to return an array, 331

structure of, 328

generic types

creating, 333-336

extending, 336-337

mixing with non-generic parameters, 340

multiple generic types, 340

queues, 334-337

type constraints, 329-332

type parameters, 328-329

placeholder types, 328-329

get keyword, 225-226, 281

getters, 225-226, 379

greater than (>) operator, 31

greater than or equal to (>=) operator, 31

guard let syntax, 371

guard statement, 121-122, 371

H

[top]

half-closed range operators, 32, 36

handling errors. See error handling

hashable, 47

Hashable protocol, 331-332

header files (Objective-C), 378-380

Healable protocol, 294

Heart struct, 294

heterogeneous collections, 353-355

hiding parameter names in initializers, 206-207

higher order functions, 127

closures, 134

function parameters, 132-134

nested functions, 130-132

returning, 128-130

homogeneous collections, 353-355

I

[top]

identical to operator, 159

identifying reference cycles, 266-269

id type, 79, 256

if statement, 59-64

C/Objective C versus Swift, 60

else-if syntax, 61-63

if-else syntax, 60-63

if let statement, 80-81

ternary conditional operators, 63-64

ignoring return values, 118

immutable variables, 7, 13

@implementation keyword, 381

implementation files (Objective-C), 380-382

implicitly unwrapped optionals, 81-82

implicit returns, 137

import Cocoa statement, 6

import UIKit statement, 6

increment operator (++), 28

indexes, 41-42

inequality (!=) comparison operator, 31

inference, type inference, 15-16

inferred type bridging, 385

infinite loops, 91

infix operators, 29

inheritance

accessing, 172-174

accessors, 231-233

base classes, 166

class identity, 176-177

defined, 165-166

initializers, 211, 218

multiple protocols, 285-290

overriding, 169-172

preventing overrides, 174-176

property accessors, 231-233

property observers, 233-235

protocols, 298

single inheritance, 172

subclasses, 167-168

when to use, 177-178

initialization

class types, 207-209, 262-263

default initializer parameters, 207-208

fallable initializers, 208-209

definition of term, 199

dictionaries, 48

initialization delegation

classes, 211-212

definition of term, 209

structures, 210-211

initialization process, 212-217

initializers, 22-23

adding with extensions, 308-309

convenience initializers, 211

default initializers, 201, 207-208

designated initializers, 211, 218

goal of, 200-201

hiding parameter names in, 206-207

inheritance, 218

inherited initializers, 211

initializer chaining, 211-215

Objective-C, 383-384

properties in structs, 151

safety, 212

value types, 22-23, 41, 201

external parameter names in initializers, 206-207

setting default values, 202-206

initializers, 22-23

adding with extensions, 308-309

convenience initializers, 211

default initializers, 201, 207-208

designated initializers, 211, 218

goal of, 200-201

hiding parameter names in, 206-207

inheritance, 218

inherited initializers, 211

initializer chaining, 211-215

init keyword, 200, 208, 383

init() method, 48, 81, 210. See also initialization

in keyword, 135

in-out parameters, 120-121

input, validating, 85-86

inserting

array elements, 43-44

set elements, 51-52

insert() method, 44

instance methods, 135

adding

to enums, 192-194

with extensions, 305-306

to structs, 152-153

comparing, 151-154

overriding, 170-171

instance properties

accessing type properties from, 240-241

compared to type properties, 258

instances

deinitializing, 261-265, 276

initializing, 262-263

instancetype keyword, 380

instance variables, 222

Int data type. See integers

integers, 17-18

converting between integers and floating-point numbers, 19

extending to adopt and conform to a protocol, 311-312

finding minimum/maximum ranges of, 17

unsigned integers, 17

when to use, 18

integrating Swift into Objective-C

creating Swift class and bridging header, 389-393

downloading starter project, 387-389

exposing Objective-C classes to Swift, 394

extending Objective-C classes with Swift, 394-395

housecleaning, 394

running app, 396-399

updating classes in storyboard, 395-396

internal access, 248

internal keyword, 248

internal parameter names, 119

intersect, 53

introducers

let, 22, 79

var, 22, 79, 281

introspection, 255

iOS playgrounds, 6

isa pointers, 176

isDisjointWith() method, 54

isEmpty property, 22, 46

is-equal operator (==), 415

is keyword, 253, 291

isSubsetOf() method, 53

isSupersetOf() method, 53

iteration

forEach function, 406-409, 419

for-in loops, 97-100

map function, 404-408, 419

@interface statement (Objective-C), 378

J-K

[top]

JSON (JavaScript Object Notation), 257

key-value pairs, 47-48

keywords

Any, 256-257

AnyObject, 256-257

as, 255-256

as!, 291

Bool, 20

break, 65, 71

class, 239

convenience, 211, 308

defer, 122, 371-374

deinit, 262

didSet, 228, 233, 236

Double, 18-19

extension, 302

fallthrough, 71-72

final, 174, 231

get, 225-226, 281

guard, 371

if, 59-64

C/Objective C versus Swift, 60

else-if syntax, 61-63

if-else syntax, 60-63

if let, 80-81

ternary conditional operators, 63-64

@implementation, 381

import, 6

in, 135

init, 200, 208, 383

instancetype, 380

Int, 17-18

@interface, 378

internal, 248

is, 253, 291

lazy, 223

let, 7, 22, 79

mutating, 158, 283, 305

nesting, 74

nil, 77

objc, 298

optional, 290

override, 169, 253, 282

private, 248

protocol, 279

public, 248

rethrows, 345

self, 153, 203, 211, 242, 357

set, 226, 281

static, 239-241, 283

String, 20-22

strong, 379

super, 172, 231

switch

break statement, 71

explicit break, 65

fall through, 65

fallthrough statement, 71-72

matching multiple values, 65-66

range matching, 66-68

syntax, 64-65

tuple matching, 68-69

where keyword, 69-70

throws, 345, 365-366

try, 367-369

try!, 369-371, 375

try?, 370-371, 375

typealias, 246-247, 304, 337

UInt, 17

unowned, 270-272

var, 7, 22, 79, 281

weak, 269

where, 69-70, 352-353

willSet, 228, 233, 236

L

[top]

last property, 46

lazy keyword, 223

lazy stored properties, 223-224, 236

leaks, memory, 276

length of functions, 108

less than (<) operator, 31, 415

less than or equal to (<=) operator, 31

let keyword, 7, 22, 79

Library class, 320

Liskov Substitution Principle, 357

lists

closure capture lists, 272-275

tasks list, removing items from, 45

literals, 19

logical operators

combining, 35

explained, 33

logical AND, 33-34

logical NOT, 35

logical OR, 34-35

loops

for loops, 94

for-condition-increment loops, 94-95

for-in loops, 96-101

infinite loops, 91

transferring control

break statement, 103

continue statements, 102-103

while loops, 89-90

conditions, 90

repeat-while loops, 90-94

while loops, 90-91

M

[top]

Mac playgrounds, 6

manipulating arrays

adding elements to, 43

inserting elements into, 43-44

removing items from, 45-46

replacing items in, 44-45

updating, 44-45

mapEachElement, 134

map function, 404-408, 419

matching

advanced pattern matching, 72-73

for-in loops, 101-102

ranges, 66-68

tuples, 68-69

MathFunction class, 273

Mavericks, Xcode compatibility, 11

maximum ranges of integers, finding, 17

mean, functions returning, 116-117

Media class, 286

median, functions returning, 116-117

median function

playgrounds, 142

rewriting, 139-140

MelonType enum, 204

memberwise initialization, 151, 155, 202-203

memory

deallocating, 261

memory leaks, 276

pointers, 22

memory leaks, 276

methods, 148. See also functions

adding with extensions, 305-306

append(), 43

area(), 175

arrays, 46

buttonTapped(), 293

calling, 322-324

classes, 155-156

configureView(), 393-395

of data types, finding, 20-21

deinit, 262

description(), 177

dictionaries, 50

exclusiveOr(), 54

factory methods, 242

getters, 225-226, 379

init(), 48, 81, 210. See also initialization

in protocols

default implementation, 298

defining, 282-284

optional methods, 290-291

insert(), 44

isDisjointWith(), 54

isSubsetOf(), 53

isSupersetOf(), 53

nested method calls, 383

nullable method parameters, 383

numberOfSectionsInTableView, 358

optional chaining, 322-324

prepareForSegue:sender:, 393

reduce instance, 136

removeAll(), 45

removeAtIndex(), 45

removeLast(), 45

removeValueForKey(), 50

self.init(), 211

setters, 226-228, 379

subscript, 251

super.init(), 218

toInt(), 23

turnItDown(), 245

typeMethodName(), 242

type methods, 242-246

updateValue(), 49-50

viewDidLoad(), 174, 393

Meyer, Bertrand, 302

Microsoft SharePoint Server, 318

minimum ranges of integers, finding, 17

minus sign (-), 29, 379

MKAnnotation protocol, 290

mode, functions returning, 116-117

modifiers, @objc, 290

module bridging (Objective-C), 384-385

modulo operator (%), 29-30

Mountain Lion, Xcode compatibility, 11

multiple bindings, validating user input with, 85-86

multiple optionals, unwrapping, 83-84

multiple protocols

adopting, 285-290

inheritance, 285-290

multiplication (*) operator, 29

mutability, 22

mutating functions, 283

mutating keyword, 158, 283, 305

N

[top]

named parameters, 190

naming conventions, 150

constants, 15

external parameter names in initializers, 206-207

parameters, 236

protocols, 284-285, 347

shorthand argument names, 138

variables, 15

Xcode projects, 4

nested functions, 130-132

nesting

method calls, 383

statements, 74

types, 309-310

nil coalescing operator, 83

nil keyword, 77

nil values

explained, 77-78

forced unwrapping, 80, 87

Objective-C

nullable method parameters, 383

nullable property definitions, 382

None, 192

nonnull attribute, 382-383

non-specific types, 256-257

NOT operator, 28, 35

NSArray class, 79, 386

NSDictionary class, 47, 79, 387

NSError class, 364

NSHipster, 417

NSMutableDictionary class, 47

NSNumber class, 386

NSString class, 386

nullability

nil values

explained, 77-78

forced unwrapping, 80, 87

Objective-C

nullable method parameters, 383

nullable property definitions, 382

_Nullable attribute, 383

null_resettable attribute, 382

_Null_unspecified attribute, 383

numberOfSectionsInTableView method, 358

numeric types, 386

O

[top]

@objc modifier, 290

ObjCInteropTests folder, 399

objc keyword, 298

Objective-C

allocation, 383-384

bridging

definition of term, 384

module bridging, 384-385

type bridging, 385-387

classes

exposing to Swift, 394

extending with Swift, 394-395

compared to Swift, 22

file structure

explained, 378

header files, 378-380

implementation files, 380-382

initialization, 383-384

in-out parameters, 121

integrating Swift into

creating Swift class and bridging header, 389-393

downloading starter project, 387-389

exposing Objective-C classes to Swift, 394

extending Objective-C classes with Swift, 394-395

housecleaning, 394

running app, 396-399

updating classes in storyboard, 395-396

nested method calls, 383

nullability

nullable method parameters, 383

nullable property definitions, 382

overview, 377

objects

flattening

flatMap function on optionals, 412-414

flatMap on sequences, 411

SharePoint Object Model, 318

observers (property)

explained, 228-230

inheriting, 233-235

overriding, 233-235

Open/Closed Principle, 302

open for extension, 301

operands, 29

operator functions, 139

operators

as?, 291

binary operators

assignment operator, 14, 30, 44

combinations of logical operators, 35

comparison operators, 31-32

compound assignment operators, 30-31

explained, 29

logical AND operator, 33-34

logical NOT operator, 35

logical OR operator, 34-35

range operators, 32-33, 36

remainder operator, 29-30

standard arithmetic operators, 29

+ (concatenation), 9

custom operators, 416-419

identical to operator, 159

infix operators, 29

optional chaining operator (?), 318

overloading, 414

custom operators, 416-419

with protocols, 415-416

overview, 27

ternary conditional operators, 35, 63-64

type cast operator, 255-256

type check operator, 253-255

unary operators

decrement operator (--), 28

explained, 27

increment operator (++), 28

logical NOT operator, 28

prefix versus postfix, 27, 36

unary minus operator, 29

unary plus operator, 29

unwrap operator, 80-81

Optical protocol, 286

optional binding, 80-81

optional chaining

definition of term, 315-316

importance of, 325

methods, 322-324

properties, 316-318

subscripts, 318-322

use cases, 316

when to use, 325

optional chaining operator (?), 318

optional keyword, 290

optional methods, 290-291

optional properties, 290-291

optionals. See optional values

optional try statement (try?), 370-371, 375

optional values, 28

additional reading, 84

designating variables as, 78-79

explained, 77-78

flattening, 412-414

pyramid of doom, 83-84

use case, 85-87

wrapping/unwrapping, 23

explained, 79-80

forced unwrapping, 80

implicitly unwrapped optionals, 81-82

multiple optionals, 83-84

nil coalescing operator, 83

optional binding, 80-81

unwrap operator, 80

optional variables, 24, 265

order of initialization (Objective-C), 382

Organization Identifier, 4

OR operator, 34-35

OS X support for Xcode, 11

out of bounds errors, 43

overloading

operators, 414

custom operators, 416-419

with protocols, 415-416

subscripts, 252-253

override keyword, 169, 253, 282

overrides, 168

default protocol implementations, 349-352

functions, 357

inherited methods, 169-172

instance methods, 170-171

preventing overrides, 231

property accessors, 231-233

property observers, 233-235

subscripts, 253

P

[top]

Paper struct, 250

parameters

closures, 137

default initializer parameters, 207-208

default parameter values, 120

external parameter names, 118-120, 206-207

function parameters, 111-112, 132-134

in-out parameters, 120-121

internal parameter names, 119

named parameters, 190

naming, 236

variadic parameters, 113-114

parent classes, 167

parentheses, 64

pattern matching. See matching

Person class, 267

Phone class, 266, 269-270

playgrounds, 6-8

creating, 6

Mac versus iOS playgrounds, 6

median function, 142

plus sign (+), 29, 379

pointers, 22

postfix unary operators, 27, 36

prefix unary operators, 27, 36

prepareForSegue:sender: method, 393

preventing overrides, 174-176, 231

print() function, 7, 329

printWeather() function, 328

private access, 248

private interfaces, 378

private keyword, 248

ProcessDelegate protocol, 372

program flow, controlling with conditionals

advanced pattern matching, 72-73

if statement, 59-64

C/Objective C versus Swift, 60

else-if syntax, 61-63

if-else syntax, 60-63

ternary conditional operators, 63-64

overview, 59

switch statement

break statement, 71

explicit break, 65

fall through, 65

fallthrough statement, 71-72

matching multiple values, 65-66

range matching, 66-68

syntax, 64-65

tuple matching, 68-69

where keyword, 69-70

ternary conditional operators, 63-64

projects

creating, 2-6

naming, 4

removing, 8

properties, 149, 221

accessors

getters, 225-226

inheriting, 231-233

overriding, 231-233

overview, 225

setters, 226-228

arrays, 46

classes, 155-156

comparing, 150-151

computed properties, 186, 224-225, 303-305

dictionaries, 50

finding, 20-21

initializing in structs, 151

nullable property definitions (Objective-C), 382

optional chaining, 316-318

property accessors, 231-233

property observers

explained, 228-230

inheriting, 233-235

overriding, 233-235

protocol properties

defining, 281-282

optional properties, 290-291

stored properties

declaring, 221-222

definition of term, 221

instance variables, 222

lazy stored properties, 223-224, 236

type properties

accessing, 239-241

compared to instance properties, 258

computed type properties, 241-242

protocol keyword, 279

protocols

adoption

adding with extensions, 310-312

definition of term, 279

multiple protocols, 285-290

syntax, 280-281

advantages of, 298

associated types, 337-339

ButtonDelegateProtocol, 293

Comparable, 415, 419

composition, 289-290

converting protocol sequences, 355-357

creating, 280-281

definition of term, 279-280

delegation, 292-297

Equatable, 415

ErrorType, 364-365, 375

explained, 343

extensions, 280

GameManagerDelegate, 295

Hashable, 331-332

Healable, 294

heterogeneous collections, 353-355

homogeneous collections, 353-355

implementing, 344

inheritance, 298

methods

default implementation, 298

defining, 282-284

optional methods, 290-291

MKAnnotation, 290

multiple protocols

adopting, 285-290

inheritance, 285-290

naming conventions, 284-285, 347

Optical, 286

overloading operators with, 415-416

overriding default protocol implementations, 349-352

ProcessDelegate, 372

properties

defining, 281-282

optional properties, 290-291

protocol composition, 289-290

protocol conformance, 279, 291

protocol extensions

CollectionType, 345

creating, 346-349

explained, 344-345

type constraints, 352-353

Rentable

creating, 280-281

methods, 282-284

STYCustomStringConvertible, 395

public access, 247-248

public interface files (Objective-C), 378

public keyword, 248

pyramid of doom, 83-84

Pythagorean Theorem, 151

Q

[top]

question mark (?), 78, 208

Queue struct

creating, 334-336

extending, 336-337

R

[top]

random number generators, 93

range operators, 32-33, 36

ranges

for-in loop, 97

range operators, 32-33, 36

switch statement, 66-68

raw values

enums, 185-186

getting, 186

setting, 186-187

initializer, 187

Ray, John, 6

Read-Eval-Print-Loop (REPL), 8-10

Rectangle class, 214

reduce function, 136

reduce instance method, 136

reference cycles

closures, 272-275

resolving, 269-272

unowned references, 270-272

weak references, 269-270

references, 78, 136, 148-149

ARC (Automatic Reference Counting), 262

closures, 272-275

how it works, 265

identifying reference cycles, 266-269

reference relationships, 265-266

resolving reference cycles, 269-272

strong references, 265

unowned references, 270-272, 276

weak references, 269-270

deallocating, 261

deinitializing, 261-265

reference count, 262

reference cycles

identifying, 266-269

resolving, 269-272

relationships, 265-266

strong references, 261, 265

versus values, 131

weak references, 269-270

relationships, reference, 265-266

remainder operator (%), 29-30

removeAll() method, 45

removeAtIndex() method, 45

removeLast() method, 45

removeValueForKey() method, 50

removing

array items, 45-46

set elements, 52-53

tasks list items, 45

Xcode projects, 8

Rentable protocol

creating, 280-281

methods, 282-284

repeat-while loops, 90-94

replacing array elements, 44-45

REPL (Read-Eval-Print-Loop), 8-10

resolving reference cycles, 269-272

unowned references, 270-272

weak references, 269-270

Result, 192-193

retain count, 262

rethrows keyword, 345

retrieving set elements, 52

returning functions

higher order functions, 128-130

syntax for, 130

return someValue, 109

return statement, 115

return types, 109, 114-118

return values, ignoring, 118

rewriting median function, 139-140

S

[top]

safety, 212

Sams Teach Yourself iOS Application Development in 24 Hours (Ray), 6

scope, 111

self.init() method, 211

self keyword, 153, 203, 211, 242, 357

semicolon (;), 22

sequences

flattening, 411

protocol sequences, converting, 355-357

SequenceType extension, 405

servers, SharePoint Server, 318

set keyword, 226, 281

sets

adding elements to, 51-52

creating, 51

definition of term, 51

performing actions on, 54

removing elements from, 52-53

retrieving elements from, 52

use case, 56

when to use, 53-55

setters, 226-228, 379

SharePoint Object Model, 318

SharePoint Server, 318

shorthand syntax

argument names, 138

enums, 187-189

single-expression closures, 137

single inheritance, 172

Site class, 319

SOLID software development principles, 357

Some, 192

SomeClassWithExpensiveOperation class, 223

SomeClassWithLazyVar class, 223

Song class, 395

sorting dictionaries, 100

Square class, 215, 232

Square struct, 226-227

StackOfPapers class, 250-251

standard arithmetic operators, 29

starter project (Objective-C), downloading, 387-389

statements. See also keywords

break, 65, 71

defer, 122, 371-374

fallthrough, 71-72

if, 59-64

C/Objective C versus Swift, 60

else-if syntax, 61-63

if-else syntax, 60-63

if let, 80-81

ternary conditional operators, 63-64

import Cocoa, 6

import UIKit, 6

@interface, 378

nesting, 74

switch

break statement, 71

explicit break, 65

fall through, 65

fallthrough statement, 71-72

matching multiple values, 65-66

range matching, 66-68

syntax, 64-65

tuple matching, 68-69

where keyword, 69-70

static dispatch, 349

static keyword, 239-241, 283

stored properties

declaring, 221-222

definition of term, 221

instance variables, 222

lazy stored properties, 223-224, 236

storyboard, updating classes in, 395-396

strings, 20-22

combining, 9

concatenating, 21

extending

with computed properties, 304

with subscripts, 306-307

for-in loops, 97-98

mutability, 22

string interpolation, 21-22

type bridging in Objective-C, 386

String type. See strings

strong keyword, 379

strongly typed languages, 256

strong reference cycles, 261, 265

closures, 272-275

identifying, 266-269

resolving, 269-272

unowned references, 270-272

weak references, 269-270

structs, 20, 147, 148

adding, 152-153

AudioCD, 284-285

BluePotion, 294

comparing to classes, 149-150, 155-156

class instance references, 159-160

differences, 157-158

instance equality, 160

instance methods, 151-154

mutating properties, 158

properties, 150-151

deinitializers and, 276

DVD, 284-285

Enemy, 294

Heart, 294

initialization, 204-206

custom initializers, 203

initialization delegation, 210-211

memberwise initialization, 202-203

memberwise initialization, 155

Paper, 250

Queue

creating, 334-336

extending, 336-337

Square, 226-227

syntax, 155

Volume, 244

when to use, 160-161

STYCustomStringConvertible protocol, 395

styMap function, 345

subclasses, 167

creating, 167-168

overriding instance methods, 170-171

subscript method, 251

subscripts, 166

adding with extensions, 306-307

arrays, 41-42

creating, 249-252

optional chaining, 318-322

overloading, 252-253

overriding, 253

subtraction (-) operator, 29

super, accessing, 172-174

superclasses, 167

overriding, 169

preventing overrides, 174-175

super.init() method, 218

super keyword, 172, 231

The Swift Programming Language, 212

switching enum values, 190-192

switch statement, 190-191

break statement, 71

explicit break, 65

fall through, 65

fallthrough statement, 71-72

matching multiple values, 65-66

range matching, 66-68

syntax, 64-65

tuple matching, 68-69

where keyword, 69-70

T

[top]

tasks list, removing items from, 45

ternary conditional operators, 35, 63-64

TestApp project, 2-6

TestClass, 263

TheLegendOfZelda class, 295

thread safe, 160

throwing errors, 365-366

throwing functions, catching errors from, 367-369

throws keyword, 345, 365-366

toInt() method, 23

tools, xcrun, 8

trailing closures, 139, 142-143

transferring

control of execution

break statement, 71

fallthrough statement, 71-72

control of loops

with break statement, 103

with continue statements, 102-103

try keyword, 367-369

try! keyword, 369-371, 375

try? keyword, 370-371, 375

tuples, 55-56, 115

for-in loops, 101

matching, 68-69

turnItDown() method, 245

type aliasing, 246-247

typealias keyword, 246-247, 304, 337

type casting

definition of term, 253-257

determining an instance’s type, 253-255

downcasting, 255-256

introspection, 255

non-specific types, 256-257

type cast operator, 255-256

type check operator, 253-255

type inference, 15-16, 48-49, 97, 137

typeMethodName() method, 242

types, 13, 16. See also extensions

access control, 247-248, 258

annotation, 16

arrays, 332

accessing values in, 41-42

appending elements to, 43

compared to dictionaries, 56

creating, 41-42

declaring, 39-41

for-in loops, 98-99

for loops, 96

functions as members of arrays, 133

generic function to return an array, 331

indexes, 41-42

inserting elements into, 43-44

member types, 40

methods, 46

properties, 46

removing items from, 45-46

replacing items in, 44-45

subscripts, 41-42

tuples, 55-56

type bridging in Objective-C, 386

updating items in, 44-45

zero-based arrays, 41

associated types, 337-339

Bool, 20, 399

constraints, 329-332, 352-353

declaring explicitly, 24

Dictionary, 332

Double, 18-19

enums, 47, 183-184, 204-206

associated values, 189-190

creating, 185-188, 192-194

custom initializers, 203-204

initialization, 204-206

raw values, 185-187

structure of, 184-185

switching values, 190-192

syntax, 187-189

error types, 364-365

finding properties/methods of, 20-21

functions, 110-111

generic types

creating, 333-336

extending, 336-337

mixing with non-generic parameters, 340

multiple generic types, 340

queues, 334-337

id, 79, 256

initialization, 201

class types, 207-209

convenience initializers, 211

default initializers, 201

definition of term, 199

designated initializers, 211

goal of initializers, 200-201

inherited initializers, 211

initialization delegation, 209-212

initialization process, 212-217

initializer chaining, 211-212

safety, 212

value types, 201-207

integers, 17-18

converting between integers and floating-point numbers, 19

extending to adopt and conform to a protocol, 311-312

finding minimum/maximum ranges of, 17

unsigned integers, 17

when to use, 18

methods

adding with extensions, 305-306

defining, 242-246

nested types, 309-310

parameters, 328-329

placeholder types, 328-329

protocol names as types, 284-285

properties

accessing, 239-241

adding with extensions, 303-305

compared to instance properties, 258

computed type properties, 241-242

strings, 20-22

combining, 9

concatenating, 21

extending, 304-307

for-in loops, 97-98

mutability, 22

string interpolation, 21-22

type bridging in Objective-C, 386

struct

custom initializers, 203

delegation initialization, 210-211

memberwise initialization, 202-203

type aliasing, 246-247

type bridging

Array and NSArray, 386

Dictionary and NSDictionary, 387

Numeric Types and NSNumber, 386

Objective-C, 385-387

String and NSString, 386

type casting

definition of term, 253

determining an instance’s type, 253-255

downcasting, 255-256

introspection, 255

non-specific types, 256-257

type inference, 15-16

U

[top]

UI development, 178

UInt data type, 17

unary minus operator, 29

unary operators

decrement operator (--), 28

explained, 27

increment operator (++), 28

logical NOT operator, 28

prefix versus postfix, 27, 36

unary minus operator, 29

unary plus operator, 29

unary plus operator, 29

underscore (_), 68, 206, 383

unions, 53

unique function, 331

unowned keyword, 270-272

unowned references, 270-272, 276

unsigned integers, 17

unwrap operator, 80-81

unwrapping optionals, 23

explained, 79-80

forced unwrapping, 80

implicitly unwrapped optionals, 81-82

multiple optionals, 83-84

nil coalescing operator, 83

optional binding, 80-81

unwrap operator, 80

updateValue() method, 49-50

updating

array elements, 44-45

classes in storyboard, 395-396

use cases

optional chaining, 316

optionals, 85-87

sets, 56

user input, validating, 85-86

V

[top]

validation

user input, 85-86

value validation, 112

values

accessing in arrays, 41-42

binding, 68

comparing, 92-93

constants

assigning values to, 14

explained, 13-14

naming, 15

when to use, 14

copying, 149

default property value, 200

default values, 202-206

enumerations, 47

explained, 78

filtering, 409-410

initialization, 22-23, 41, 201

external parameter names in initializers, 206-207

setting default values, 202-206

literals, 19

nil values

explained, 77-78

forced unwrapping, 80, 87

Objective-C, 382-383

optional values, 28

additional reading, 84

designating variables as, 78-79

explained, 77-78

flattening, 412-414

pyramid of doom, 83-84

use case, 85-87

wrapping/unwrapping, 23, 79-82

struct, 20

value binding, 68

variables, 14-15

versus references, 131

value types, 148

value validation, 112

variables

creating, 79

declaring, 14-15, 22-23

designating as optionals, 78-79

explained, 14-15

instance variables, 222

naming, 15

optionals

additional reading, 84

designating, 78-79

explained, 77-78

pyramid of doom, 83-84

wrapping/unwrapping, 23, 79-84

optional variables, 24, 265

variadic parameters, 113-114

var keyword, 7, 22, 79, 281

verifying protocol conformance, 291

view controllers, 173-174

viewDidLoad() method, 174, 393

Volume struct, 244

W

[top]

weak keyword, 269

weak references, 269-270

websites

Apple documentation, 417

NSHipster, 417

Welcome to Xcode screen, 3

where keyword, 69-70, 87, 352-353

while loops, 89-91

conditions, 90

repeat-while loops, 90-94

repeat-while loops, 92-93

wildcards, 68, 101

willSet keyword, 228, 233, 236

wrappers, 78

wrapping optionals, 79-80

X-Y-Z

[top]

Xcode

Auto Complete, 394

downloading, 2

playgrounds, 6-8

projects

creating, 2-6

naming, 4

removing, 8

raw value initializer, 187

xcrun, 8

Yosemite, support for Xcode, 11

zero-based arrays, 41