Swift Apprentice
Swift Apprentice
About the authors
About the editors
Who this book is for
What you need
How to use this book
What's in store
Book source code and forums
Book updates
License
Acknowledgments
About the cover
How a computer works
Representing numbers
How code works
Playgrounds
Creating a playground
Playgrounds overview
Key points
Where to go from here?
Code comments
Printing out
Arithmetic operations
Simple operations
Decimal numbers
The remainder operation
Shift operations
Order of operations
Math functions
Naming data
Constants
Variables
Using meaningful names
Increment and decrement
Mini-exercises
Key points
Where to go from here?
Challenges
Type conversion
Operators with mixed types
Type inference
Mini-exercises
Strings
How computers represent strings
Unicode
Strings in Swift
Characters and strings
Concatenation
Interpolation
Mini-exercises
Tuples
Mini-exercises
A whole lot of number types
A peek behind the curtains: Protocols
Key points
Where to go from here?
Challenges
Comparison operators
Boolean operators
Boolean logic
String equality
Mini-exercises
The if statement
Short circuiting
Encapsulating variables
The ternary conditional operator
Mini-exercises
Loops
While loops
Repeat-while loops
Breaking out of a loop
Mini-exercises
Key Points
Where to go from here?
Challenges
Ranges
For loops
Continue and labeled statements
Mini-exercises
Switch statements
Advanced switch statements
Partial matching
Mini-exercises
Key points
Where to go from here?
Challenges
Function basics
Function parameters
Return values
Advanced parameter handling
Overloading
Mini-exercises
Functions as variables
The land of no return
Key points
Where to go from here?
Challenges
Challenge A: Looping with stride functions
Challenge B: It’s prime time
Challenge C: Recursive functions
Introducing nil
Sentinel values
Introducing optionals
Mini-exercises
Unwrapping optionals
Force unwrapping
Optional binding
Mini-exercises
Introducing guard
Nil coalescing
Key points
Where to go from here?
Challenges
Challenge A: You be the compiler
Challenge B: Divide and conquer
Challenge C: Refactor and reduce
Challenge D: Nested optionals
Introducing big-O notation
What is an array?
When are arrays useful?
Mutable versus immutable arrays
Creating arrays
Accessing elements
Using properties and methods
Using subscripting
Using ranges
Checking for an element
Modifying Arrays
Appending elements
Inserting elements
Removing elements
Updating elements
Moving elements
Iterating through an array
Variadic Parameters
Running time for array operations
Key points
Where to go from here?
Challenges
Creating dictionaries
Accessing values
Using subscripting
Using properties and methods
Modifying dictionaries
Adding pairs
Mini-exercise
Updating values
Removing pairs
Iterating through dictionaries
Running time for dictionary operations
Key points
Where to go from here?
Challenges
Closure basics
Shorthand syntax
Closures with no return value
Capturing from the enclosing scope
Custom sorting with closures
Iterating over collections with closures
Mini-exercises
Key points
Where to go from here?
Challenges
Challenge A: Repeating yourself
Challenge B: Closure sums
Challenge C: Functional ratings
Introducing structures
Your first structure
Mini-exercise
Accessing members
Mini-exercise
Introducing methods
Mini-exercise
Structures as values
Structs everywhere
Conforming to a protocol
Key points
Where to go from here?
Challenges
Stored properties
Default values
Computed properties
Mini-exercise
Getter and setter
Type properties
Property observers
Limiting a variable
Mini-exercise
Lazy properties
Mini-exercise
Key points
Where to go from here?
Challenges
Challenge Set A: We all scream for ice cream
Challenge Set B:
Method refresher
Comparing methods to computed properties
Turning a function into a method
Introducing self
Mini-exercise
Introducing initializers
Initializers in structures
Introducing mutating methods
Type methods
Mini-exercise
Adding to an existing structure with extensions
Keeping the compiler generated initializer using extensions
Key points
Where to go from here?
Challenges
Creating classes
Reference types
The heap vs. the stack
Working with references
Mini-exercise
Object identity
Mini-exercise
Methods and mutability
Mutability and constants
Mini-exercise
Understanding state and side effects
Extending a class using an extension
When to use a class versus a struct
Key points
Where to go from here?
Challenges
Challenge A: Movie lists — benefits of reference types
Challenge B: T-Shirt store — class or struct?
Introducing inheritance
Polymorphism
Runtime hierarchy checks
Inheritance, methods and overrides
Introducing super
When to call super
Preventing inheritance
Inheritance and class initialization
Two-phase initialization
Mini-exercise
Required and convenience initializers
Mini-exercise
When and why to subclass
Single responsibility
Strong types
Shared base classes
Extensibility
Identity
Understanding the class lifecycle
Deinitialization
Mini-exercises
Retain cycles and weak references
Key points
Where to go from here?
Challenges
Your first enumeration
Declaring an enumeration
Deciphering an enumeration in a function
Mini-exercise
Code completion prevents typos
Raw values
Accessing the raw value
Initializing with the raw value
Mini-exercise
String raw values
Unordered raw values
Mini-exercise
Associated values
Enumeration as state machine
Mini-exercise
Case-less enumerations
Mini-exercise
Optionals
Key points
Where to go from here?
Challenges
Challenge A: Adding raw values
Challenge B: Computing with raw values
Challenge C: Pattern matching enumeration values
Introducing protocols
Protocol syntax
Methods in protocols
Properties in protocols
Initializers in protocols
Protocol inheritance
Mini-exercises
Implementing protocols
Implementing properties
Associated types in protocols
Implementing multiple protocols
Extensions and protocol conformance
Requiring reference semantics
Protocols in the standard library
Equatable
Comparable
"Free" functions
Other useful protocols
Key points
Where to go from here?
Challenges
Challenge A: Bike protocols
Challenge B: Pet shop tasks
Introducing generics
Values defined by other values
Types defined by other types
Anatomy of generic types
Using type parameters
Mini-exercises
Type constraints
Type inference with generics
Arrays
Dictionaries
Optionals
Generic function parameters
Key points
Where to go from here?
Challenges
Challenge A: Building a collection
Problems introduced by lack of access control
Introducing access control
Private
Playground sources
Fileprivate
Internal, public, and open
Mini-exercises
Organizing code into extensions
Extensions by behavior
Extensions by protocol conformance
Key points
Challenges
Challenge A: Singleton pattern
Challenge B: Stack
Challenge C: Character battle
Custom operators
Types of operators
Exponentiation operator
Compound assignment operator
Mini-exercises
Generic operators
Precedence and associativity
Subscripts
Key points
Where to go from here?
Challenges
Introducing patterns
Basic pattern matching
if and guard
switch
Mini exercise
for
Patterns
Wildcard pattern
Value-binding pattern
Identifier pattern
Tuple pattern
Enumeration case pattern
Mini exercise
Optional pattern
“Is” type-casting pattern
“As” type-casting pattern
Advanced patterns
Qualifying with where
Chaining with commas
Custom tuple
Fun with wildcards
Programming exercises
Expression pattern
Overloading ~=
Key points
Challenges
Challenge A
Challenge B
Challenge C
Challenge D
What is error handling?
First level error handling with optionals
Failable initializers
Optional chaining
Map and flatmap
Error protocol
Throwing errors
Handling errors
Not looking at the detailed error
Stoping your program on an error
Advanced error handling
PugBot
Handling multiple errors
Rethrows
Key points
Where to go from here?
Challenges
Reference cycles for classes
Weak references
Unowned references
Reference cycles for closures
Capture lists
Unowned self
Handling asynchronous closures
GCD
Weak self
The strong weak pattern
Key points
Where to go from here?
Challenges
Challenge A
Challenge B
Value types vs. reference types
Reference types
Value types
Defining value semantics
When to prefer value semantics
Implementing value semantics
Case 1. Primitive value types
Case 2. Composite value types
Case 3. Reference types
Case 4. Value types containing mutable reference types
Recipes for value semantics
Key Points
Where to go from here?
Challenges
Challenge A
Challenge B
Challenge C
Introducing protocol extensions
Default implementations
Mini-exercise
Understanding protocol extension dispatching
Type constraints
Mini-exercise
Protocol-oriented benefits
Programming to interfaces, not implementations
Traits, mixins, and multiple inheritance
Simplicity
Why Swift is a protocol-oriented language
Key points
Where to go from here?
Challenges
Challenge A: Protocol extension practice
Challenge B: Randomization
Powered by
GitBook
Mini-exercise
Mini-exercise
Rewrite isInDeliveryRange to use Location and DeliveryArea.
results matching "
"
No results matching "
"