O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Swift 2 Programming, Second Edition

Book Description

Get started fast with Swift 2 programming for iOS and OS X

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

Author Jacob Schatz’s popular YouTube video tutorials 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.

Learn how to:

  • 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 levels with advanced operators

  • Implement efficient design patterns with protocols and delegates

  • Create animated 2D games with SpriteKit, and apps with UIKit

  • 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. 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: Variables, Constants, and Loops
      1. Building Blocks of Swift
        1. Computed Properties (Getters and Setters)
        2. Using Comments
        3. Inference
      2. Merging Variables into a String
      3. Optionals: A Gift to Unwrap
        1. Printing Your Results
        2. Implicitly Unwrapped Optionals
      4. Tuples
      5. Number Types
      6. From Objective-C to Swift
        1. Control Flow: Making Choices
        2. Switching It Up: switch Statements
        3. Stop...Hammer Time
      7. Summary
    13. 2. Collecting Your Data: Arrays and Dictionaries
      1. Using Arrays
        1. Your First Array the Long Way
        2. A Quicker Array
        3. Using AnyObject
        4. 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. SpriteKit
      1. Introducing SpriteKit
        1. The SKNode and SKSpriteNode
      2. Creating a Game
        1. The New Project Screen
        2. The Game
      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. Creating Your Own Syntax: 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. The Game
      2. The Setup
      3. Tour the Code
      4. The Game
        1. Step 1: Create the World
        2. Step 2: Making Things Move
      5. Summary
    22. 11. Making Games with Physics
      1. Making a Physics-Based Game
        1. Creating the Project
        2. Adding the Assets
        3. Adding the Levels
        4. Generating the Levels
        5. Making a Playable Game
        6. Creating the Cage
      2. Summary
    23. 12. Making Apps with UIKit
      1. Application Types
        1. Single-View Applications
        2. Creating the User Interface
        3. Adding Constraints
        4. Hooking Up the UI to Code
        5. Writing the Code
        6. The TableView
      2. Summary
    24. Index
    25. Code Snippets