You are previewing Swift Programming: The Big Nerd Ranch Guide.
O'Reilly logo
Swift Programming: The Big Nerd Ranch Guide

Book Description

Through the authors' carefully constructed explanations and examples, you will develop an understanding of Swift grammar and the elements of effective Swift style. Written for Swift 2.0, the book is also compatible with Swift 2.1. You will also learn how to navigate Xcode 7 and get the most out of Apple's documentation. Throughout the book, the authors share their insights into Swift to ensure that you understand the hows and whys of Swift and can put that understanding to use in different contexts.
 

After working through this book, you will have the knowledge and confidence to develop your own solutions to a wide range of programming challenges using Swift.

Table of Contents

  1. Title Page
  2. Dedication
  3. Acknowledgments
  4. Table of Contents
  5. Introduction
    1. Learning Swift
    2. Whither Objective-C?
    3. Prerequisites
    4. How This Book Is Organized
    5. How to Use This Book
    6. Challenges
    7. For the More Curious
    8. Typographical Conventions
    9. Necessary Hardware and Software
    10. Using an eBook
    11. Before We Begin
  6. I. Getting Started
    1. 1. Getting Started
      1. Getting Started with Xcode
      2. Playing in a Playground
      3. Varying Variables and Printing to the Console
      4. You Are On Your Way!
      5. Bronze Challenge
    2. 2. Types, Constants, and Variables
      1. Types
      2. Constants vs. Variables
      3. String Interpolation
      4. Bronze Challenge
  7. II. The Basics
    1. 3. Conditionals
      1. if/else
      2. Ternary Operator
      3. Nested ifs
      4. else if
      5. Bronze Challenge
    2. 4. Numbers
      1. Integers
      2. Creating Integer Instances
      3. Operations on Integers
        1. Integer division
        2. Operator shorthand
        3. Overflow operators
      4. Converting Between Integer Types
      5. Floating-Point Numbers
      6. Bronze Challenge
    3. 5. Switch
      1. What Is a Switch?
      2. Switch It Up
        1. Ranges
        2. Value binding
        3. where clauses
        4. Tuples and pattern matching
          1. Pattern matching
      3. switch vs. if/else
      4. Bronze Challenge
    4. 6. Loops
      1. for-in Loops
        1. for case
      2. A Quick Note on Type Inference
      3. for Loops
      4. while Loops
      5. repeat-while Loops
      6. Control Transfer Statements, Redux
      7. Bronze Challenge
    5. 7. Strings
      1. Working with Strings
      2. Unicode
        1. Unicode scalars
        2. Canonical equivalence
          1. Counting elements
          2. Indices and ranges
      3. Silver Challenge
    6. 8. Optionals
      1. Optional Types
      2. Optional Binding
      3. Implicitly Unwrapped Optionals
      4. Optional Chaining
      5. Modifying an Optional in Place
      6. The Nil Coalescing Operator
      7. Silver Challenge
  8. III. Collections and Functions
    1. 9. Arrays
      1. Creating an Array
      2. Accessing and Modifying Arrays
      3. Array Equality
      4. Immutable Arrays
      5. Documentation
      6. Bronze Challenge
      7. Silver Challenge
    2. 10. Dictionaries
      1. Creating a Dictionary
      2. Populating a Dictionary
      3. Accessing and Modifying a Dictionary
      4. Adding and Removing Values
      5. Looping
      6. Immutable Dictionaries
      7. Translating a Dictionary to an Array
      8. Silver Challenge
    3. 11. Sets
      1. What Is a Set?
      2. Getting a Set
      3. Working with Sets
        1. Unions
        2. Intersects
        3. Disjoint
      4. Bronze Challenge
      5. Silver Challenge
    4. 12. Functions
      1. A Basic Function
      2. Function Parameters
        1. Parameter names
        2. Variadic parameters
        3. Default parameter values
        4. In-out parameters
      3. Returning from a Function
      4. Nested Functions and Scope
      5. Multiple Returns
      6. Optional Return Types
      7. Exiting Early from a Function
      8. Function Types
      9. Bronze Challenge
      10. Silver Challenge
    5. 13. Closures
      1. Closure Syntax
      2. Closure Expression Syntax
      3. Functions as Return Types
      4. Functions as Arguments
      5. Closures Capture Values
      6. Closures Are Reference Types
      7. Functional Programming
        1. Higher-order functions
          1. map(_:)
          2. filter(_:)
          3. reduce(_:combine:)
      8. Gold Challenge
  9. IV. Enumerations, Structures, and Classes
    1. 14. Enumerations
      1. Basic Enumerations
      2. Raw Value Enumerations
      3. Methods
      4. Associated Values
      5. Recursive Enumerations
      6. Bronze Challenge
      7. Silver Challenge
    2. 15. Structs and Classes
      1. A New Project
      2. Structures
      3. Instance Methods
        1. Mutating methods
      4. Classes
        1. A monster class
        2. Inheritance
          1. A zombie subclass
          2. Preventing overriding
          3. Your town has a zombie problem
      5. Method Parameter Names
      6. What Should I Use?
      7. Bronze Challenge
      8. Silver Challenge
      9. For the More Curious: Type Methods
      10. For the More Curious: Function Currying
    3. 16. Properties
      1. Basic Stored Properties
      2. Nested Types
      3. Lazy Stored Properties
      4. Computed Properties
        1. A getter and a setter
      5. Property Observers
      6. Type Properties
      7. Access Control
        1. Controlling getter and setter visibility
      8. Bronze Challenge
      9. Silver Challenge
      10. Gold Challenge
    4. 17. Initialization
      1. Initializer Syntax
      2. Struct Initialization
        1. Default initializers for structs
        2. Custom initializers for structs
          1. Initializer delegation
      3. Class Initialization
        1. Default initializers for classes
        2. Initialization and class inheritance
          1. Automatic initializer inheritance
          2. Designated initializers for classes
          3. Convenience initializers for classes
        3. Required initializers for classes
        4. Deinitialization
      4. Failable Initializers
        1. A failable Town initializer
        2. Failable initializers in classes
      5. Initialization Going Forward
      6. Silver Challenge
      7. Gold Challenge
      8. For the More Curious: Initializer Parameters
    5. 18. Value vs. Reference Types
      1. Value Semantics
      2. Reference Semantics
      3. Constant Value and Reference Types
      4. Using Value and Reference Types Together
        1. Immutable reference types
      5. Copying
      6. Identity vs. Equality
      7. What Should I Use?
  10. V. Advanced Swift
    1. 19. Protocols
      1. Formatting a Table of Data
      2. Protocols
      3. Protocol Conformance
      4. Protocol Inheritance
      5. Protocol Composition
      6. Mutating Methods
      7. Silver Challenge
      8. Gold Challenge
    2. 20. Error Handling
      1. Classes of Errors
      2. Lexing an Input String
      3. Catching Errors
      4. Parsing the Token Array
      5. Handling Errors by Sticking Your Head in the Sand
      6. Swift Error Handling Philosophy
      7. Bronze Challenge
      8. Silver Challenge
      9. Gold Challenge
    3. 21. Extensions
      1. Extending an Existing Type
      2. Extending Your Own Type
        1. Use extensions to add protocol conformance
        2. Adding an initializer with an extension
        3. Nested types and extensions
        4. Extensions with functions
      3. Bronze Challenge
      4. Bronze Challenge
      5. Silver Challenge
    4. 22. Generics
      1. Generic Data Structures
      2. Generic Functions and Methods
      3. Type Constraints
      4. Associated Type Protocols
      5. Type Constraint where Clauses
      6. Bronze Challenge
      7. Silver Challenge
      8. Gold Challenge
      9. For the More Curious: Understanding Optionals
      10. For the More Curious: Parametric Polymorphism
    5. 23. Protocol Extensions
      1. Modeling Exercise
      2. Extending ExerciseType
      3. Protocol Extension where Clauses
      4. Default Implementations with Protocol Extensions
      5. Naming Things: A Cautionary Tale
      6. Bronze Challenge
      7. Gold Challenge
    6. 24. Memory Management and ARC
      1. Memory Allocation
      2. Strong Reference Cycles
      3. Reference Cycles in Closures
      4. Bronze Challenge
      5. Silver Challenge
      6. For the More Curious: Can I Retrieve the Reference Count of an Instance?
    7. 25. Equatable and Comparable
      1. Conforming to Equatable
      2. Conforming to Comparable
      3. Comparable’s Inheritance
      4. Bronze Challenge
      5. Gold Challenge
      6. Platinum Challenge
      7. For the More Curious: Custom Operators
  11. VI. Event-Driven Applications
    1. 26. Your First Cocoa Application
      1. Getting Started with VocalTextEdit
      2. Model-View-Controller
      3. Setting Up the View Controller
      4. Setting Up Views in Interface Builder
        1. Adding the Speak and Stop buttons
        2. Adding the text view
        3. Auto Layout
      5. Making Connections
        1. Setting target-action pairs for VocalTextEdit’s buttons
        2. Connecting the text view outlet
      6. Making VocalTextEdit… Vocal
      7. Saving and Loading Documents
        1. Type casting
        2. Saving documents
        3. Loading documents
        4. MVC cleanup
      8. Silver Challenge
      9. Gold Challenge
    2. 27. Your First iOS Application
      1. Getting Started with iTahDoodle
      2. Laying Out the User Interface
        1. Wiring up your interface
      3. Modeling a To-Do List
      4. Setting Up the UITableView
      5. Saving and Loading TodoList
        1. Saving TodoList
        2. Loading TodoList
      6. Bronze Challenge
      7. Silver Challenge
      8. Gold Challenge
    3. 28. Interoperability
      1. An Objective-C Project
        1. Creating a contacts app
      2. Adding Swift to an Objective-C Project
        1. Adding contacts
      3. Adding an Objective-C Class
      4. Silver Challenge
      5. Gold Challenge
  12. 29. Conclusion
    1. Where to Go from Here?
    2. Shameless Plugs
    3. An Invitation
  13. Index