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 Int Double Numeric coercion Other numeric types Arithmetic operations Comparison
3.7.3. String
3.7.4. Character
3.7.5. Range
3.7.6. Tuple
3.7.7. Optional Unwrapping an Optional Implicitly unwrapped Optional The magic word nil Optional chains Comparison with Optional Why Optionals?
4. Object Types
4.1. Object Type Declarations and Features
4.1.1. Initializers Optional properties Referring to self Delegating initializers 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 Kinds of class initializer Subclass initializers 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 Suppressing type checking Object identity
4.11.3. AnyClass
4.12. Collection Types
4.12.1. Array Array casting and type testing Array comparison Arrays are value types Array subscripting Nested arrays Basic array properties and methods Array enumeration and transformation Swift Array and Objective-C NSArray
4.12.2. Dictionary Basic dictionary properties and enumeration Swift Dictionary and Objective-C NSDictionary
4.12.3. Set Option sets Swift Set and Objective-C NSSet
5. Flow Control and More
5.1. Flow Control
5.1.1. Branching If construct Conditional binding Switch statement Conditional evaluation
5.1.2. Loops While loops For loops
5.1.3. Jumping Shortcircuiting and labels Throwing and catching errors Nested scopes Defer Aborting the whole program 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