You are previewing Sams Teach Yourself Swift in 24 Hours, Second Edition.
O'Reilly logo
Sams Teach Yourself Swift in 24 Hours, Second Edition

Book Description

In just 24 lessons of one hour or less, Sams Teach Yourself Swift in 24 Hours, Second Edition, will teach you how to build next-generation OS X and iOS apps with Apple’s Swift 2 programming language. This book’s straightforward, step-by-step approach helps you quickly master Swift 2’s core concepts, structure, and syntax and use Swift to write safe, powerful, modern code. In just a few hours you’ll be applying features such as extensions, closures, protocols, and generics. Every lesson builds on what you’ve already learned, giving you a rock-solid foundation for real-world success.

Step-by-step instructions carefully walk you through the most common Swift 2 programming tasks.
Practical, hands-on examples show you how to apply what you learn.
Quizzes and exercises help you test your knowledge and stretch your skills.
Notes and tips point out shortcuts and solutions.

Learn how to...

  • Get started with Swift 2.0’s Xcode 7 development environment

  • Master Swift’s fundamental data types

  • Use operators to change, assign, combine, check, or verify values

  • Retain data and preferences in memory with arrays, sets, and dictionaries

  • Control program flow, modify execution paths, and iterate code

  • Perform complex actions concisely with Swift 2.0’s improved functions

  • Work with higher-order functions and closures

  • Harness the power of structs, enums, classes, and class inheritance

  • Take advantage of Swift’s advanced memory allocation and references

  • Use protocols to define behavior and explore Protocol-Oriented Programming

  • Add type functionality with extensions

  • Smoothly handle errors

  • Leverage the power of generics to create flexible and reusable code

  • Interoperate with Objective-C code

  • Get started with Functional Programming approaches and thinking functionally

  • Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Contents at a Glance
    5. Table of Contents
    6. About the Author
    7. Dedication
    8. Acknowledgments
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
      1. Who Should Read This Book?
      2. What Should You Expect from This Book?
    12. Hour 1. Introducing the Swift Development Environment
      1. What Is Swift?
      2. Getting Started
        1. Take a Look Around
        2. Xcode Playgrounds
        3. The Swift Read-Eval-Print-Loop (REPL)
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    13. Hour 2. Learning Swift’s Fundamental Data Types
      1. Constants in Swift
      2. Variables in Swift
      3. Introducing Data Types
        1. Type Inference
        2. Data Types
        3. Initializing Values
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Answers
      7. Exercise
    14. Hour 3. Using Operators in Swift
      1. Unary Operators
        1. Increment and Decrement Operators
        2. Logical NOT Operator
        3. Unary Minus Operator
      2. Binary Operators
        1. Standard Arithmetic Operators
        2. Remainder Operator
        3. The Assignment Operator
        4. Compound Assignment Operators
        5. Comparison Operators
        6. Range Operators
        7. Logical Operators
      3. Ternary Conditional Operators
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Answers
      7. Exercise
    15. Hour 4. Working with Collection Types
      1. Arrays
        1. Indexes and Subscripts for Accessing Values
        2. Manipulating Arrays
        3. Common Array Methods and Properties
      2. Dictionaries
        1. Key-Value Pairs
        2. Initializing Dictionaries
        3. Type Inference with Dictionaries
        4. Adding Data to Dictionaries
        5. Removing Pairs from Dictionaries
        6. Common Dictionary Methods and Properties
      3. Sets
        1. Creating a Set
      4. Tuples
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Answers
      8. Exercise
    16. Hour 5. Controlling Program Flow with Conditionals
      1. The if Statement
        1. Ternary Conditional Operators
      2. The switch Statement
        1. Match More Than Just Int Values
        2. Switch Range Matching
        3. Switch Tuple Matching
        4. Refining Switch Cases with the where Keyword
        5. Transferring Control of Execution
        6. Slightly More Advanced Pattern Matching
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    17. Hour 6. Understanding Optional Values
      1. What Are Optional Values?
      2. How to Designate a Variable as Optional
      3. Wrapping and Unwrapping Optional Variables
        1. Forced Unwrapping with the Unwrap Operator
        2. Optional Binding to Unwrap Variables
        3. Implicitly Unwrapped Optionals
        4. Nil Coalescing Operator
        5. Unwrapping Multiple Optionals
      4. Use Case for Optionals
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Answers
      8. Exercise
    18. Hour 7. Iterating Code with Loops
      1. Two Categories of Loops
        1. While Loops
        2. For Loops
      2. Transferring Control in Loops
        1. Transferring Control with the continue Statement
        2. Transferring Control with the break Statement
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    19. Hour 8. Using Functions to Perform Actions
      1. The Nature of Functions in Swift
      2. General Function Syntax and Structure
      3. Functions with No Parameters and No Return Type
      4. The Type of a Function
      5. Functions with Parameters
      6. Functions with Variadic Parameters
      7. Functions with Return Types
      8. External Parameter Names
      9. Default Parameter Values
      10. Change Argument Values with In-Out Parameters
      11. Exiting Early
      12. Deferring Execution
      13. Summary
      14. Q&A
      15. Workshop
        1. Quiz
        2. Answers
      16. Exercise
    20. Hour 9. Understanding Higher Order Functions and Closures
      1. Higher Order Functions
        1. Returning Function Types
        2. Nesting Functions within Functions
        3. Using Functions as Function Parameters
      2. Closures
        1. Structure of Closures
        2. Using Trailing Closures
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    21. Hour 10. Learning About Structs and Classes
      1. Overview of Structs and Classes in Swift
      2. What Swift Structs and Classes Have in Common
        1. Defining Properties
        2. Instance Methods
        3. Struct and Class Similarities
      3. Differences Between Structs and Classes
        1. Mutating Struct Properties
        2. Comparing Class Reference Equality
        3. Comparing Instance Equality
      4. When to Use a Class or a Struct
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Answers
      8. Exercise
    22. Hour 11. Implementing Class Inheritance
      1. What Is Inheritance?
      2. Identifying a Base Class
      3. Creating a Subclass
      4. Overriding Inherited Methods
      5. Accessing super
      6. Preventing Overrides
      7. Class Identity
      8. When to Use Class Inheritance
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Answers
      12. Exercise
    23. Hour 12. Harnessing the Power of Enums
      1. Understanding Swift Enums
      2. Swift Enum Structure
      3. Raw Values
        1. Getting Raw Values from Enums
        2. Setting Enum Values from Raw Values
      4. Enum Shorthand Syntax
      5. Associated Values
      6. Switching Enum Values
      7. Adding Instance Methods to Enums
      8. Summary
      9. Q&A
      10. Workshop
        1. Quiz
        2. Answers
      11. Exercise
    24. Hour 13. Customizing Initializers of Classes, Structs, and Enums
      1. Initialization
        1. The Goal of Initializers
        2. There Is Always an Initializer
      2. Initializing Value Types
        1. Setting Default Values
        2. External Parameter Names in Initializers
        3. Initializing Class Types
      3. Advanced Initialization
        1. Initialization Delegation
        2. Class Initialization Delegation
        3. The Initialization Process
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Answers
      7. Exercise
    25. Hour 14. Digging Deeper with Properties
      1. Stored Properties
        1. Instance Variables
        2. Lazy Stored Properties
      2. Computed Properties
      3. Property Accessors
        1. Getters
        2. Setters
      4. Property Observers
      5. Inheriting and Overriding Accessors
      6. Inheriting and Overriding Observers
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Answers
      10. Exercise
    26. Hour 15. Adding Advanced Type Functionality
      1. Type Properties and Methods
        1. Type Properties
        2. Computed Type Properties
        3. Type Methods
      2. Type Aliasing
      3. Type Access Control
      4. Subscripts
        1. Subscript Overloading
        2. Overriding Subscripts
      5. Type Casting and Non-Specific Types
        1. Determining an Instance’s Type
        2. Downcasting
        3. Non-Specific Types
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Answers
      9. Exercise
    27. Hour 16. Understanding Memory Allocation and References
      1. Deinitialization
      2. Automatic Reference Counting
        1. How ARC Works
        2. Reference Relationships and Behaviors
        3. Reference Cycles
        4. Resolving Strong Reference Cycles
        5. Closures and Strong Reference Cycles
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    28. Hour 17. Using Protocols to Define Behavior
      1. Defining Protocols
      2. Creating and Adopting Protocols
      3. Properties
      4. Defining Methods in Protocols
      5. Using Protocol Names as Types
      6. Adopting and Inheriting Multiple Protocols
      7. Optional Protocol Properties and Methods
      8. How to Check for Protocol Conformance
      9. Using Protocols for Delegation
      10. Summary
      11. Q&A
      12. Workshop
        1. Quiz
        2. Answers
      13. Exercise
    29. Hour 18. Using Extensions to Add Type Functionality
      1. Defining Extensions
        1. Basic Structure of Extensions
      2. Adding Functionality with Extensions
        1. Computed Instance and Type Properties
        2. Instance and Type Methods
        3. Adding Subscripts in Extensions
        4. Adding Custom Initializers with Extensions
        5. Adding Nested Types to Extensions
        6. Protocol Adoption and Conformance in Extensions
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    30. Hour 19. Working with Optional Chaining
      1. Defining Optional Chaining
      2. Chaining Optional Properties
      3. Subscripts
      4. Methods
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Answers
      8. Exercise
    31. Hour 20. Introducing Generics
      1. An Introduction to Generics
      2. Type Parameters and Placeholder Types
      3. Specifying Type Constraints
      4. Creating Generic Types
      5. Extending Generic Types
      6. Using Associated Types in Protocols
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Answers
      10. Exercise
    32. Hour 21. Understanding Protocol-Oriented Programming
      1. A Quick Recap of Protocols
      2. Implementing Protocols
      3. Introducing Protocol Extensions
      4. Creating Protocol Extensions
      5. What Is and Isn’t Customizable
      6. Type Constraints in Protocol Extensions
      7. Homogeneous and Heterogeneous Collections
      8. Converting Protocol Sequences
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Answers
      12. Exercise
    33. Hour 22. Handling Errors
      1. Error Handling
        1. Cocoa Error Handling
      2. Swift Error Handling
        1. Customizing Error Types
        2. Throwing Errors
        3. Catching Errors
        4. Deferring Execution
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    34. Hour 23. Adding Interoperability with Objective-C
      1. Objective-C Basics
        1. File Structure
        2. Nullability
        3. Allocation and Initialization
      2. Bridging
        1. Module Bridging
        2. Type Bridging
      3. Integrating Swift into an Objective-C App
        1. Download the Starter Project
        2. Create a Swift Class and Bridging Header
        3. Housecleaning
        4. Exposing Objective-C Classes to Swift
        5. Extending an Objective-C Class with Swift
        6. Update the Class in the Storyboard
        7. Run the App
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Answers
      7. Exercise
    35. Hour 24. Functional Thinking in Swift
      1. What Is Functional Programming?
      2. Thinking in Terms of Functions
        1. Transforming with map
        2. Iterating with forEach
        3. Filtering Values
        4. Chaining Functions Together
        5. Flattening Objects
        6. Creating and Overloading Operators
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
      6. Exercise
    36. Index
    37. Code Snippets