You are previewing Learning Swift™ Programming.
O'Reilly logo
Learning Swift™ Programming

Book Description

Get started fast with Swiftprogrammingfor iOS and OS X

Learning Swift Programming is a fast-paced, hands-on introduction to writing production-quality iOS and OS X apps with Apple’s new programming language. Written for developers with previous experience in any other modern language, this book explains Swift simply and clearly, using relevant examples that solve realistic problems.

Author Jacob Schatz’s popular Skip Wilson video tutorials on YouTube have already helped thousands of Apple developers get started with Swift. Now, he helps you take full advantage of Swift’s advanced design, remarkable performance, and streamlined development techniques.

Step-by-step, you’ll move from basic syntax through advanced features such as closures and generics–discovering helpful tips and tricks along the way. After you’ve mastered Swift’s building blocks and learned about its key innovations, a full section of case studies walks you through building complete apps from scratch.

  • Compare Swift with Objective-C, JavaScript, Python, Ruby, and C

  • Collect data with arrays and dictionaries, and store it with variables and constants

  • Group commonly-used code into functions for easy reuse

  • Structure your code with enums, structs, and classes

  • Use generics to get more done with less code

  • Write closures to share small blocks of functionality

  • Use optionals to write more robust, crash-resistant, and cleaner code

  • Integrate existing Objective-C code into new Swift apps

  • Program on the bit and byte level with advanced operators

  • Implement efficient design patterns with protocols and delegates

  • Create animated 2D games with SpriteKit, and 3D games with SceneKit

  • Contents at a Glance

    1 Getting Your Feet Wet
        Building Blocks of Swift
        Optionals: A Gift to Unwrap
        Tuples
        Number Types and Converting Between Them
        Coming to Swift from Objective-C and C

    2 Collecting Data
        Using Arrays
        Modifying Arrays
        Using Dictionaries

    3 Making Things Happen: Functions
        Defining Functions
        More on Parameters

    4 Structuring Code: Enums, Structs, and Classes
        Enums
        Structs

    5 Making a Game
        Building a User Interface (UI)
        The Action-Packed View Controller

    6 Reusable Code: Closures
        What Are Closures?
        Closures in Other Languages
        How Closures Work and Why They’re Awesome

    7 Subscripts and Advanced Operators
        Writing Your First Subscript
        Bits and Bytes with Advanced Operators
        Customizing Operators
        Making Your Own Operators
        Bits and Bytes in Real Life

    8 Protocols
        Writing Your First Protocol
        Animizable and Humanizable
        Delegation
        Protocols as Types
        Protocols in Collections
        Optional Chaining

    9 Becoming Flexible with Generics
        The Problem That Generics Solve

    10 Games with SpriteKit
         Setting Up the Project
         The Start Screen
         Dangerous Ground
         A Hero to the Rescue
         Enemies in Motion
         Spawned Obstacles
         Smashing Physics

    11 Games with SceneKit
         Creating DAE Files
         Creating a New SceneKit Project
         Your SceneKit Files
         Making the Game
         Bridging the Gap to Objective-C

    12 Apps with UIKit
         Application Types
         Loading a Table View
         Loading Data from a URL

    Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents at a Glance
    6. Table of Contents
    7. About the Author
    8. Acknowledgments
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
      1. Who Should Read This Book
      2. Why You Should Read This Book
      3. What You Will Learn from This Book
      4. What Is Swift?
      5. How This Book Is Organized
      6. Enjoy the Ride
    12. 1. Getting Your Feet Wet
      1. Building Blocks of Swift
        1. Using Comments
        2. Inference Everywhere!
      2. Optionals: A Gift to Unwrap
        1. Printing Your Results
        2. Implicitly Unwrapped Optionals
      3. Tuples
      4. Number Types and Converting Between Them
      5. Coming to Swift from Objective-C and C
        1. Control Flow: Making Choices
        2. Switching It Up: switch Statements
        3. Stop...Hammer Time
      6. Summary
    13. 2. Collecting Data
      1. Using Arrays
        1. Your First Array
        2. A Quicker Array
        3. Using AnyObject
        4. What’s an Array Made Of?
        5. Differences Between NSArrays and Swift Arrays
      2. Modifying Arrays
        1. Accessing Array Elements
        2. Adding Elements to an Array
        3. Removing Elements from Arrays
        4. Iterating Over Arrays
        5. Extra Bits of Arrays
        6. Emptying an Array
      3. Using Dictionaries
        1. Adding, Removing, and Inserting with Dictionaries
        2. Iterating Over Dictionaries
        3. Extra Bits of Dictionaries
        4. Emptying a Dictionary
        5. Testing Dictionaries for the Presence of Values
        6. Putting It All Together
      4. Summary
    14. 3. Making Things Happen: Functions
      1. Defining Functions
        1. Return Types
        2. Multiple Return Values
      2. More on Parameters
        1. External Parameter Names
        2. Default Parameter Values
        3. Variadic Parameters
        4. In-Out Parameters
        5. Functions as Types
        6. Putting It All Together
      3. Summary
    15. 4. Structuring Code: Enums, Structs, and Classes
      1. Enums
        1. Which Member Was Set?
        2. Associated Values
        3. Raw Values
      2. Structs
        1. Defining Methods in Structs
        2. Structs Are Always Copied
        3. Mutating Methods
        4. Classes
        5. Initialization
        6. What Is a Reference Type?
        7. Do I Use a Struct or a Class?
        8. Forgot Your Pointer Syntax?
        9. Property Observers
        10. Methods in Classes
      3. Summary
    16. 5. Making a Game
      1. Building a User Interface (UI)
        1. Importing Assets
        2. Adding Buttons and Labels
      2. The Action-Packed View Controller
        1. Displaying the Player’s Moves
        2. Developing the Concept for an AI
        3. Managing the Game State
        4. Implementing the AI Logic
        5. Finding Victory Conditions
        6. Moving In for the Win
        7. Defending the AI’s Turf and Stealing Center Stage
        8. Taking Sides
        9. When It’s a Draw
      3. Summary
    17. 6. Reusable Code: Closures
      1. What Are Closures?
      2. Closures in Other Languages
      3. How Closures Work and Why They’re Awesome
        1. The Closure Syntax
        2. Inferring Using Context
        3. Arguments Have a Shorthand, Too
        4. Sorting a Custom Car Class
        5. Closures Are Reference Types
        6. Automatic Reference Counting
        7. Strong Reference Cycles
        8. Trailing Closures
      4. Summary
    18. 7. Subscripts and Advanced Operators
      1. Writing Your First Subscript
      2. Bits and Bytes with Advanced Operators
        1. Bitwise NOT
        2. Bitwise AND
        3. Bitwise OR
        4. Bitwise XOR
        5. Shifting Bits
        6. UInt8, UInt16, UInt32, Int8, Int16, Int32, and So On
        7. Value Overflow and Underflow
      3. Customizing Operators
      4. Making Your Own Operators
      5. Bits and Bytes in Real Life
      6. Summary
    19. 8. Protocols
      1. Writing Your First Protocol
        1. Properties
      2. Animizable and Humanizable
        1. Methods
      3. Delegation
      4. Protocols as Types
      5. Protocols in Collections
        1. Protocol Inheritance
        2. Protocol Composition
        3. Protocol Conformity
        4. Optional Protocol Prerequisites
      6. Optional Chaining
        1. Back to Optional Protocol Requisites
        2. Useful Built-in Swift Protocols
      7. Summary
    20. 9. Becoming Flexible with Generics
      1. The Problem That Generics Solve
        1. Other Uses for Generics
        2. Generics for Protocols
        3. The where Clause
      2. Summary
    21. 10. Games with SpriteKit
      1. Setting Up the Project
        1. Loading a Scene View
        2. Importing Image Assets
      2. The Start Screen
        1. Custom Colors
        2. Handling Taps
      3. Dangerous Ground
        1. Positioning Obstacles
        2. Moving Obstacles
      4. A Hero to the Rescue
        1. Rotating Nodes
        2. Bar Hopping
      5. Enemies in Motion
        1. Elements of Chance
        2. Tracking a Brick
        3. Tracking All Blocks
      6. Spawned Obstacles
        1. Generating Barriers
        2. Hardcore Mode
      7. Smashing Physics
        1. Invisible Cages
        2. A Hero’s Death
      8. Summary
    22. 11. Games with SceneKit
      1. Creating DAE Files
      2. Creating a New SceneKit Project
      3. Your SceneKit Files
        1. Setting the Stage
        2. Importing the Assets
        3. Material Properties
      4. Making the Game
        1. Adding a Floor
        2. Adding the Level
        3. Placing Each Block
        4. Creating a Hero
      5. Bridging the Gap to Objective-C
        1. Back to the Hero
        2. Adding the Controls
      6. Summary
    23. 12. Apps with UIKit
      1. Application Types
        1. Single-View Applications
        2. Adding Constraints
        3. Adding a New View
      2. Loading a Table View
      3. Loading Data from a URL
      4. Summary
    24. Index
    25. Code Snippets