You are previewing Swift for Beginners: Develop and Design, Second Edition.
O'Reilly logo
Swift for Beginners: Develop and Design, Second Edition

Book Description

LEARNING A NEW PROGRAMMING LANGUAGE can be daunting. With Swift, Apple has lowered the barrier of entry for developing iOS and OS X apps by giving developers an innovative programming language for Cocoa and Cocoa Touch. Now in its second edition, Swift for Beginners has been updated to accommodate the evolving features of this rapidly adopted language.

If you are new to Swift, this book is for you. If you have never used C, C++, or Objective-C, this book is definitely for you. With this handson guide, you’ll quickly be writing Swift code, using Playgrounds to instantly see the results of your work. Author Boisy G. Pitre gives you a solid grounding in key Swift language concepts—including variables, constants, types, arrays, and dictionaries—before he shows you how to use Swift’s innovative Xcode integrated development environment to create apps for iOS and OS X.

THIS BOOK INCLUDES:

  • Detailed instruction, ample illustrations, and clear examples

  • Best practices from an experienced Mac and iOS developer

  • Emphasis on how to use Xcode, Playgrounds, and the REPL

  •  COMPANION WEBSITE:

    www.peachpit.com/swiftbeginners2 includes additional resources.

    Table of Contents

    1. Title Page
    2. Copyright Page
    3. Dedication Page
    4. Acknowledgments
    5. About the Author
    6. Contents
    7. Introduction
      1. Who Is This Book For?
      2. How to Use This Book
        1. How You Will Learn
        2. What You Will Learn
    8. Welcome to Swift
      1. The Technologies
    9. Section I: The Basics
      1. Chapter 1. Introducing Swift
        1. Evolutionary Yet Revolutionary
        2. Preparing for Success
          1. Tools of the Trade
          2. Interacting with Swift
        3. Ready, Set...
        4. Diving into Swift
          1. Help and Quit
          2. Hello, World!
        5. The Power of Declaration
        6. Constants Are Consistent
        7. This Thing Called a Type
          1. Testing the Limits
          2. Can a Leopard Change Its Stripes?
          3. Being Explicit
        8. Strings and Things
          1. Stringing Things Together
          2. Characters Have Character
        9. Math and More
          1. Expressions
          2. Mixing Numeric Types
          3. Numeric Representations
        10. True or False
          1. The Result
        11. Printing Made Easy
        12. Using Aliases
        13. Grouping Data with Tuples
        14. Optionals
        15. Summary
      2. Chapter 2. Working with Collections
        1. The Candy Jar
          1. Birds of a Feather...
          2. Extending the Array
          3. Replacing and Removing Values
          4. Inserting Values at a Specific Location
          5. Combining Arrays
        2. The Dictionary
          1. Looking Up an Entry
          2. Adding an Entry
          3. Updating an Entry
          4. Removing an Entry
        3. Arrays of Arrays?
        4. Starting from Scratch
          1. The Empty Array
          2. The Empty Dictionary
        5. Iterating Collections
          1. Array Iteration
          2. Dictionary Iteration
        6. Summary
      3. Chapter 3. Taking Control
        1. For What It’s Worth
          1. Counting on It
          2. Inclusive or Exclusive?
          3. For Old Time’s Sake
          4. Writing Shorthand
        2. It’s Time to Play
        3. Making Decisions
          1. The Decisiveness of “If”
          2. When One Choice Is Not Enough
          3. Switching Things Around
          4. While You Were Away...
          5. Inspecting Your Code
          6. Give Me a Break!
        4. Summary
      4. Chapter 4. Writing Functions and Closures
        1. The Function
          1. Coding the Function in Swift
          2. Exercising the Function
          3. More Than Just Numbers
          4. Parameters Ad Nauseam
          5. Functions Fly First Class
          6. Throw Me a Function, Mister
          7. A Function in a Function in a...
          8. Default Parameters
          9. What’s in a Name?
          10. When It’s Good Enough
          11. To Use or Not to Use?
          12. Don’t Change My Parameters!
          13. The Ins and Outs
        2. Bringing Closure
        3. Summing It Up
        4. Stay Classy
      5. Chapter 5. Organizing with Classes and Structures
        1. Objects Are Everywhere
        2. Swift Objects Are Classy
          1. Knock, Knock
          2. Let There Be Objects!
          3. Opening and Closing the Door
          4. Locking and Unlocking the Door
          5. Examining the Properties
          6. Door Diversity
          7. Painting the Door
        3. Inheritance
          1. Modeling the Base Class
          2. Creating the Subclasses
          3. Instantiating the Subclass
          4. Convenience Initializers
        4. Enumerations
        5. Structural Integrity
        6. Value Types vs. Reference Types
        7. Looking Back, Looking Ahead
      6. Chapter 6. Formalizing with Protocols and Extensions
        1. Following Protocol
          1. Class or Protocol?
          2. More Than Just Methods
          3. Adopting Multiple Protocols
          4. Protocols Can Inherit, Too
          5. Delegation
        2. Extending With Extensions
          1. Extending Basic Types
          2. Using Closures in Extensions
        3. Summary
    10. Section II: Developing with Swift
      1. Chapter 7. Working with Xcode
        1. Xcode’s Pedigree
        2. Creating Your First Swift Project
        3. Diving Down
          1. Interacting with the Project Window
          2. It’s Alive!
        4. Piquing Your Interest
          1. Making Room
          2. Building the UI
          3. Tidying Up
          4. Class Time
          5. Hooking It Up
        5. You Made an App!
      2. Chapter 8. Making a Better App
        1. It’s the Little Things
          1. Show Me the Money
          2. Remember the Optional?
          3. Unwrapping Optionals
          4. Looking Better
          5. Formatting: A Different Technique
        2. Compounding
          1. Hooking Things Up
          2. Testing Your Work
        3. When Things Go Wrong
          1. Where’s the Bug?
          2. At the Breaking Point
          3. The Confounding Compound
        4. The Value of Testing
          1. The Unit Test
          2. Crafting a Test
          3. When Tests Fail
          4. Tests That Always Run
        5. Wrapping Up
      3. Chapter 9. Going Mobile with Swift
        1. In Your Pocket vs. on Your Desk
        2. How’s Your Memory?
          1. Thinking About Gameplay
          2. Designing the UI
        3. Creating the Project
        4. Building the User Interface
          1. Creating the Buttons
          2. Running in the Simulator
          3. Setting Constraints
        5. The Model-View-Controller
        6. Coding the Game
          1. The Class
          2. Enumerations
          3. The View Objects
          4. The Model Objects
          5. Overridable Methods
          6. Game Methods
          7. Winning and Losing
        7. Back to the Storyboard
        8. Time to Play
      4. Chapter 10. Becoming an Expert
        1. Memory Management in Swift
          1. Value vs. Reference
          2. The Reference Count
          3. Only the Strong Survive
          4. Put It in a Letter
          5. The Test Code
          6. Breaking the Cycle
          7. Cycles in Closures
          8. Thanks for the Memories
        2. Thinking Logically
          1. To Be or NOT to Be...
          2. Combining with AND
          3. One Way OR the Other
        3. Generics
        4. Overloading Operators
        5. Equal vs. Identical
        6. Error Handling
          1. Throwing an Error
          2. Catching the Error
        7. Scripting and Swift
          1. Creating the Script
          2. Setting Permissions
          3. Executing the Script
          4. Examining How It Works
        8. Calling S.O.S.
        9. Game Time
      5. Chapter 11. Heading Downhill
        1. Gaming the System
          1. GameKit
          2. SpriteKit
        2. It Starts with an Idea
          1. Heading Downhill
          2. Social Connectivity
        3. Ready, Set...
          1. How the Game Plays
          2. Take It for a Spin
        4. Inspecting the Project
          1. Classes
          2. Assets
          3. Scenes
        5. Touring the Source
          1. The Home Scene
          2. The Game Scene
          3. Game View Controller
          4. Taking It All In
        6. You Did It!
          1. Study Apple’s Frameworks
          2. Join Apple’s Developer Program
          3. Become a Part of the Community
          4. Never Stop Learning
          5. Bon Voyage!
    11. Index
    12. Code Snippets