You are previewing Swift for the Really Impatient.
O'Reilly logo
Swift for the Really Impatient

Book Description

Swift for the Really Impatient is the perfect jumpstart to the Swift language for every experienced Objective-C developer. Leading iOS and OS X developers Matt Henderson and Dave Wood help you take full advantage of Swift’s powerful innovations, without wasting time on basics you already know.

Through expert code examples and crystal-clear explanations, Henderson and Wood reveal how Swift features improve on Objective-C and show how to make the most of them. You’ll quickly master “Swift-er” techniques for using objects, classes, optionals, generics, functions, closures, and more. Each chapter includes hands-on exercises designed to reinforce and deepen your skills.

You’ll learn how to re-implement common Objective-C programming solutions in Swift, improving code clarity, power, and performance on both desktop and mobile devices. And, because you probably won’t (or can’t) abandon Objective-C anytime soon, you’ll discover how to use both languages together, smoothly integrate Swift into existing projects and workflows, and gradually move your current code base into the future. Coverage includes

  • Using Swift’s new patterns and concise, expressive syntax to produce safer, more reliable code

  • Leveraging the surprising power of Swift’s data structures

  • Utilizing Swift’s type system to help prevent errors common in other languages

  • Using optionals to correctly handle invalid, missing, or unknown values

  • Implementing generics to reduce duplication, improve power, and simplify maintenance

  • Interacting with C and Objective-C APIs

  • Abstracting functionality for reusable code while maintaining type safety

  • Isolating code in clean, flexible, low-overhead closures

  • Combining Swift with Objective-C in the same project

  • Avoiding gotchas when importing Objective-C classes in Swift projects

  • Nesting types to improve code clarity

  • Creating shared objects with singletons

  • Managing threading and concurrency with Swift’s advanced Grand Central Dispatch (GCD) patterns

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents
    6. Foreword
    7. Preface
    8. Acknowledgments
    9. About the Authors
    10. Chapter 1. Introducing Swift
      1. 1.1 Basic Syntax
        1. 1.1.1 Variables and Constants
        2. 1.1.2 String Interpolation
        3. 1.1.3 Control Flow
      2. 1.2 Basic Data Types
        1. 1.2.1 Int
        2. 1.2.2 Double and Float
        3. 1.2.3 Bool
        4. 1.2.4 Strings
        5. 1.2.5 Arrays
        6. 1.2.6 Dictionaries
      3. Exercises
    11. Chapter 2. Diving Deeper into Swift’s Syntax
      1. 2.1 Optionals
      2. 2.2 Generics
      3. 2.3 Type Annotations and Type Inference
      4. 2.4 Functions and Closures
        1. 2.4.1 Global Functions
        2. 2.4.2 Nested Functions
        3. 2.4.3 Closure Expressions
      5. 2.5 Tuples
      6. 2.6 switch Statements and Pattern Matching
      7. Exercises
    12. Chapter 3. Objects and Classes
      1. 3.1 Enumerations
      2. 3.2 Classes
      3. 3.3 Structures
      4. 3.4 Subclassing
      5. 3.5 Overloading
      6. 3.6 Overriding
      7. 3.7 Initialization
      8. 3.8 Properties
        1. 3.8.1 Computed Properties
        2. 3.8.2 Property Observers
        3. 3.8.3 lazy Properties
      9. 3.9 Subscripting
      10. 3.10 Protocols
      11. 3.11 Extensions
      12. 3.12 Access Control
      13. Exercises
    13. Chapter 4. Optionals
      1. 4.1 Optionals and nil
      2. 4.2 Validity Checking, Optional Binding, and Forced Unwrapping
      3. 4.3 Optional Chaining
      4. 4.4 Implicitly Unwrapped Optionals
      5. Exercises
    14. Chapter 5. Generics
      1. 5.1 Why Generics?
      2. 5.2 Generic Functions
        1. 5.2.1 Type Parameters
        2. 5.2.2 Using More Than One Type Parameter
      3. 5.3 Generic Types
        1. 5.3.1 Associated Types
        2. 5.3.2 The where Clause
      4. Exercises
    15. Chapter 6. Functions and Closures
      1. 6.1 Functions
        1. 6.1.1 Parameters
        2. 6.1.2 External Parameter Names
        3. 6.1.3 Default Parameter Values
        4. 6.1.4 Variadic and inout Parameters
        5. 6.1.5 Return Types
      2. 6.2 Closures
        1. 6.2.1 Inferring Parameters and the Return Value
        2. 6.2.2 Trailing Closures
      3. 6.3 Functional Programming Patterns
      4. Exercises
    16. Chapter 7. Working with Objective-C
      1. 7.1 Interacting with C and Objective-C APIs
        1. 7.1.1 Optional Properties and Return Values
        2. 7.1.2 AnyObject Types
        3. 7.1.3 Subclassing, Extensions, and Protocols
        4. 7.1.4 Automatically Converted Types
        5. 7.1.5 Selectors and Enums
        6. 7.1.6 Working with C Code
      2. 7.2 Working with Swift and Objective-C in a Single Project
      3. Exercises
    17. Chapter 8. Common Patterns
      1. 8.1 Nested Types
      2. 8.2 Singletons
      3. 8.3 Using Grand Central Dispatch
        1. 8.3.1 dispatch_once
        2. 8.3.2 dispatch_async
        3. 8.3.3 dispatch_after
        4. 8.3.4 dispatch_apply
      4. 8.4 Sequences and Generators
      5. 8.5 Operators
        1. 8.5.1 Operator Overloading
        2. 8.5.2 Custom Operators
      6. Exercises
    18. Index
    19. Code Snippets