This is an introductory explanation of the Swift 3 programming language as it relates to practical real-life iOS programming. Copyright 2016 Matt Neuburg. It constitutes a portion of my book, iOS 10 Programming Fundamentals with Swift. If you find these excerpts useful, please consider buying the book!

iOS 10 Programming Fundamentals with Swift


Table of Contents

1. The Architecture of Swift
1.1. Ground of Being
1.2. Everything Is an Object?
1.3. Three Flavors of Object Type
1.4. Variables
1.5. Functions
1.6. The Structure of a Swift File
1.7. Scope and Lifetime
1.8. Object Members
1.9. Namespaces
1.10. Modules
1.11. Instances
1.12. Why Instances?
1.13. self
1.14. Privacy
1.15. Design
1.15.1. Object Types and APIs
1.15.2. Instance Creation, Scope, and Lifetime
1.15.3. Summary and Conclusion
2. Functions
2.1. Function Parameters and Return Value
2.1.1. Void Return Type and Parameters
2.1.2. Function Signature
2.2. External Parameter Names
2.3. Overloading
2.4. Default Parameter Values
2.5. Variadic Parameters
2.6. Ignored Parameters
2.7. Modifiable Parameters
2.8. Function In Function
2.9. Recursion
2.10. Function As Value
2.11. Anonymous Functions
2.12. Define-and-Call
2.13. Closures
2.13.1. How Closures Improve Code
2.13.2. Function Returning Function
2.13.3. Closure Setting a Captured Variable
2.13.4. Closure Preserving Its Captured Environment
2.13.5. Escaping Closures
2.14. Curried Functions
2.15. Function References and Selectors
2.15.1. Function Reference Scope
2.15.2. Selectors
3. Variables and Simple Types
3.1. Variable Scope and Lifetime
3.2. Variable Declaration
3.3. Computed Initializer
3.4. Computed Variables
3.5. Setter Observers
3.6. Lazy Initialization
3.7. Built-In Simple Types
3.7.1. Bool
3.7.2. Numbers
3.7.2.1. Int
3.7.2.2. Double
3.7.2.3. Numeric coercion
3.7.2.4. Other numeric types
3.7.2.5. Arithmetic operations
3.7.2.6. Comparison
3.7.3. String
3.7.4. Character
3.7.5. Range
3.7.6. Tuple
3.7.7. Optional
3.7.7.1. Unwrapping an Optional
3.7.7.2. Implicitly unwrapped Optional
3.7.7.3. The magic word nil
3.7.7.4. Optional chains
3.7.7.5. Comparison with Optional
3.7.7.6. Why Optionals?
4. Object Types
4.1. Object Type Declarations and Features
4.1.1. Initializers
4.1.1.1. Optional properties
4.1.1.2. Referring to self
4.1.1.3. Delegating initializers
4.1.1.4. Failable initializers
4.1.2. Properties
4.1.3. Methods
4.1.4. Subscripts
4.1.5. Nested Object Types
4.1.6. Instance References
4.2. Enums
4.2.1. Raw Values
4.2.2. Associated Values
4.2.3. Enum Initializers
4.2.4. Enum Properties
4.2.5. Enum Methods
4.2.6. Why Enums?
4.3. Structs
4.3.1. Struct Initializers, Properties, and Methods
4.3.2. Struct As Namespace
4.4. Classes
4.4.1. Value Types and Reference Types
4.4.2. Subclass and Superclass
4.4.3. Class Initializers
4.4.3.1. Kinds of class initializer
4.4.3.2. Subclass initializers
4.4.3.3. Required initializers
4.4.4. Class Deinitializer
4.4.5. Class Properties and Methods
4.5. Polymorphism
4.6. Casting
4.7. Type Reference
4.8. Protocols
4.8.1. Why Protocols?
4.8.2. Protocol Type Testing and Casting
4.8.3. Declaring a Protocol
4.8.4. Optional Protocol Members
4.8.5. Class Protocol
4.8.6. Implicitly Required Initializers
4.8.7. Literal Convertibles
4.9. Generics
4.9.1. Generic Declarations
4.9.2. Type Constraints
4.9.3. Explicit Specialization
4.9.4. Associated Type Chains
4.9.5. Additional Constraints
4.10. Extensions
4.10.1. Extending Object Types
4.10.2. Extending Protocols
4.10.3. Extending Generics
4.11. Umbrella Types
4.11.1. Any
4.11.2. AnyObject
4.11.2.1. Suppressing type checking
4.11.2.2. Object identity
4.11.3. AnyClass
4.12. Collection Types
4.12.1. Array
4.12.1.1. Array casting and type testing
4.12.1.2. Array comparison
4.12.1.3. Arrays are value types
4.12.1.4. Array subscripting
4.12.1.5. Nested arrays
4.12.1.6. Basic array properties and methods
4.12.1.7. Array enumeration and transformation
4.12.1.8. Swift Array and Objective-C NSArray
4.12.2. Dictionary
4.12.2.1. Basic dictionary properties and enumeration
4.12.2.2. Swift Dictionary and Objective-C NSDictionary
4.12.3. Set
4.12.3.1. Option sets
4.12.3.2. Swift Set and Objective-C NSSet
5. Flow Control and More
5.1. Flow Control
5.1.1. Branching
5.1.1.1. If construct
5.1.1.2. Conditional binding
5.1.1.3. Switch statement
5.1.1.4. Conditional evaluation
5.1.2. Loops
5.1.2.1. While loops
5.1.2.2. For loops
5.1.3. Jumping
5.1.3.1. Shortcircuiting and labels
5.1.3.2. Throwing and catching errors
5.1.3.3. Nested scopes
5.1.3.4. Defer
5.1.3.5. Aborting the whole program
5.1.3.6. Guard
5.2. Operators
5.3. Privacy
5.3.1. Private and Fileprivate
5.3.2. Public and Open
5.3.3. Privacy Rules
5.4. Introspection
5.5. Memory Management
5.5.1. Weak References
5.5.2. Unowned References
5.5.3. Weak and Unowned References in Anonymous Functions
5.5.4. Memory Management of Protocol-Typed References
A. C, Objective-C, and Swift
A.1. The C Language
A.1.1. C Data Types
A.1.2. C Enums
A.1.3. C Structs
A.1.4. C Pointers
A.1.5. C Arrays
A.1.6. C Functions
A.2. Objective-C
A.2.1. Objective-C Objects and C Pointers
A.2.2. Objective-C Objects and Swift Objects
A.2.3. Bridged Types and Boxed Types
A.2.4. Objective-C Methods
A.2.5. Objective-C Initializers and Factories
A.2.6. Selectors
A.2.7. CFTypeRefs
A.2.8. Blocks
A.2.9. API Markup
A.3. Bilingual Targets

List of Figures

1.1. Making an instance and calling an instance method
1.2. Two dogs with different property values
1.3. A stack
3.1. Quick Help displays a variable’s type
4.1. Part of the Cocoa class hierarchy as shown in Xcode

List of Examples

1.1. Schematic structure of a legal Swift file
5.1. The Swift if construct
5.2. The Swift switch statement
5.3. The Swift while loop
5.4. The Swift for loop
5.5. The Swift do...catch construct
5.6. The Swift guard statement