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

Swift Essentials - Second Edition

Book Description

Discover how to build iOS and watchOS applications in Swift 2 using Xcode

About This Book

  • Gets you up and running with Swift programming without any prior iOS development experience.
  • A fast paced guide showing best practices and lets you get up to speed with Swift to quickly build your own iOS applications
  • A unique practical approach to make your life with Swift easy.

Who This Book Is For

Are you interested in learning Swift? Do you want to write iOS applications in Swift? If yes, then this is the book for you. No prior iOS programming experience is assumed; however, having some experience with any programming language will be beneficial.

What You Will Learn

  • Dive into Swift and explore its innovative and powerful syntax
  • Work with Swift in Xcode to get a unique and productive approach to development
  • Find out how to create complete iOS applications
  • Discover rapid prototyping with a Swift playground
  • Get to know how to use the Swift storyboard to develop multi-page applications
  • Get to grips with parsing JSON and XML data from network sources
  • Build a network client for GitHub repositories, with full source code on GitHub

In Detail

Swift was considered one of the biggest innovations last year, and certainly with Swift 2 announced at WWDC in 2015, this segment of the developer space will continue to be hot and dominating.

This is a fast-paced guide to provide an overview of Swift programming and then walks you through in detail how to write iOS applications. Progress through chapters on custom views, networking, parsing and build a complete application as a Git repository, all by using Swift as the core language

Style and approach

This fast-paced practical guide will quickly give you hands-on experience with all the features of Swift programming. Following the practical examples in the book will help you successfully create your own iOS applications.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Swift Essentials Second Edition
    1. Table of Contents
    2. Swift Essentials Second Edition
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewer
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Trademarks
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Exploring Swift
      1. Open source Swift
      2. Getting started with Swift
        1. Numeric literals
        2. Floating point literals
        3. String literals
        4. Variables and constants
        5. Collection types
        6. Optional types
        7. Nil coalescing operator
      3. Conditional logic
        1. If statements
        2. Switch statements
      4. Iteration
        1. Iterating over keys and values in a dictionary
        2. Iteration with for loops
        3. Break and continue
      5. Functions
        1. Named arguments
        2. Optional arguments and default values
        3. Guards
        4. Multiple return values and arguments
        5. Returning structured values
        6. Error handling
        7. Throwing errors
        8. Catching errors
        9. Cleaning up after errors
      6. Command-line Swift
        1. Interpreted Swift scripts
        2. Compiled Swift scripts
      7. Summary
    10. 2. Playing with Swift
      1. Getting started with playgrounds
        1. Creating a playground
        2. Viewing the console output
        3. Viewing the timeline
      2. Displaying objects with Quick Look
        1. Showing colored labels
        2. Showing images
      3. Advanced techniques
        1. Capturing values explicitly
        2. Running asynchronous code
      4. Playgrounds and documentation
        1. Learning with playgrounds
        2. Understanding the playground format
        3. Adding a page
        4. Documenting code
          1. Playground navigation documentation
          2. Text formatting
          3. Symbol documentation
      5. Limitations of playgrounds
      6. Summary
    11. 3. Creating an iOS Swift App
      1. Understanding iOS applications
      2. Creating a single-view iOS application
        1. Removing the storyboard
        2. Setting up the view controller
      3. Swift classes, protocols, and enums
        1. Classes in Swift
        2. Subclasses and testing in Swift
        3. Protocols in Swift
        4. Enums in Swift
          1. Raw values
          2. Associated values
      4. Creating a master-detail iOS application
        1. The AppDelegate class
        2. The MasterViewController class
        3. The DetailViewController class
      5. Summary
    12. 4. Storyboard Applications with Swift and iOS
      1. Storyboards, scenes, and segues
        1. Creating a storyboard project
        2. Scenes and view controllers
        3. Adding views to the scene
        4. Segues
      2. Adding a navigation controller
        1. Naming scenes and views
      3. Swift and storyboards
        1. Custom view controllers
        2. Connecting views to outlets in Swift
        3. Calling actions from interface builder
        4. Triggering a segue with code
        5. Passing data with segues
      4. Using Auto Layout
        1. Understanding constraints
        2. Adding constraints
          1. Adding a constraint with drag and drop
          2. Adding constraints to the Press Me scene
          3. Adding missing constraints
      5. Summary
    13. 5. Creating Custom Views in Swift
      1. An overview of UIView
      2. Creating new views with Interface Builder
        1. Creating a table view controller
        2. Showing data in the table
        3. Defining a view in a xib file
        4. Wiring a custom view class
        5. Dealing with intrinsic size
      3. Creating new views by subclassing UIView
        1. Auto Layout and custom views
        2. Constraints and the visual format language
        3. Adding the custom view to the table
      4. Custom graphics with drawRect
        1. Drawing graphics in drawRect
        2. Responding to orientation changes
      5. Custom graphics with layers
        1. Creating a ProgressView from layers
        2. Adding the stop square
        3. Adding a progress bar
        4. Clipping the view
        5. Testing views in Xcode
        6. Responding to change
      6. Summary
    14. 6. Parsing Networked Data
      1. Loading data from URLs
        1. Dealing with errors
        2. Dealing with missing content
        3. Nested if and switch statements
        4. Networking and user interfaces
        5. Running functions on the main thread
      2. Parsing JSON
        1. Handling errors
      3. Parsing XML
        1. Creating a parser delegate
        2. Downloading the data
        3. Parsing the data
      4. Direct network connections
        1. Opening a stream-based connection
        2. Synchronous reading and writing
          1. Writing data to NSOutputStream
          2. Reading from an NSInputStream
          3. Reading and writing hexadecimal and UTF8 data
          4. Implementing the Git protocol
          5. Listing git references remotely
          6. Integrating the network call into the UI
        3. Asynchronous reading and writing
          1. Reading data asynchronously from an NSInputStream
          2. Creating a stream delegate
          3. Dealing with errors
          4. Listing references asynchronously
          5. Displaying asynchronous references in the UI
          6. Writing data asynchronously to an NSOutputStream
      5. Summary
    15. 7. Building a Repository Browser
      1. An overview of the GitHub API
        1. Root endpoint
        2. User resource
        3. Repositories resource
      2. Repository browser project
        1. URI templates
        2. Background threading
        3. Parsing JSON dictionaries
        4. Parsing JSON arrays of dictionaries
      3. Creating the client
        1. Talking to the GitHub API
        2. Returning repositories for a user
        3. Accessing data through the AppDelegate
      4. Accessing repositories from view controllers
        1. Adding users
        2. Implementing the detail view
        3. Transitioning between the master and detail views
        4. Loading the user's avatar
        5. Displaying the user's avatar
      5. Summary
    16. 8. Adding Watch Support
      1. Watch applications
        1. Adding a watch target
        2. Adding the GitHubAPI to the watch target
      2. Creating watch interfaces
        1. Adding a list of users to the watch
        2. Wiring up the interface
        3. Adding an image
      3. Responding to user interaction
        1. Adding context and showing repositories
        2. Adding a detail screen
        3. Populating the detail screen
      4. Best practice for watch applications
        1. UI thread considerations
        2. Stored data
        3. Appropriate use of complications and glances
      5. Summary
    17. A. References to Swift-related Websites, Blogs, and Notable Twitter Users
      1. Language
      2. Twitter users
      3. Blogs and tutorial sites
      4. Meetups
      5. Afterword
    18. Index