You are previewing Swift Recipes: A Problem-Solution Approach.
O'Reilly logo
Swift Recipes: A Problem-Solution Approach

Book Description

Swift Recipes provides a problem solution approach for dealing with key aspects of the Swift programming language (covering version 1.2), ensuring you have the indispensable reference you need to successfully execute common programming tasks. You’ll learn how to use the unique features of the Swift programming language as well as its use with Cocoa and Cocoa touch frameworks and libraries.

Solutions are available for a range of problems, including application development with Xcode; working with strings, numbers, and object collections; dealing with threads, multi-core processing, and asynchronous processing; and building applications that take advantage of dates and timers and memory management.

This book is an essential core reference for every Swift programmer and offers solutions in a concise and easy-to-follow manner. T. Michael Rogers has developed iOS applications for Fortune 100 brands and startups, and has trained new and experienced iOS developers via the iOS Boot Camp in New York City, online courses, and in private settings. He brings his expertise to offer you the ability to use and exploit Swift to get the most out of all your projects for your app creations, whether you use iOS or Mac OS X.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. Chapter 1: Swift Programming
    1. 1-1. Getting Started with Swift
      1. Problem
      2. Solution
    2. 1-2. Installing Xcode 6
      1. Problem
      2. Solution
      3. How It Works
    3. 1-3. Working with Playgrounds
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 1-4. Designing User Interface Elements in a Storyboard
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 1-5. Dealing with Strings
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 1-6. Formatting Numbers as Strings
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 1-7. Getting the Length of a Swift String
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 1-8. Manipulating Swift Strings
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 1-9. Manipulating Strings with Native Swift Methods
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    10. 1-10. Storing Strings on the iOS File System
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    11. 1-11. Reading a Text File into a String
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    12. 1-12. Reading and Writing Text Files in Cocoa
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    13. 1-13. Dealing with Numbers
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    14. 1-14. Dealing with Dates
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  12. Chapter 2: Complex Types
    1. 2-1. Writing Functions
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 2-2. Creating Classes
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 2-3. Adding Class Properties
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 2-4. Initializing Class Properties
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 2-5. Adding Class Methods
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 2-6. Inheriting from Classes
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 2-7. Implementing Protocols
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 2-8. Setting Property Observers
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 2-9. Defining Enumerations
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    10. 2-10. Creating Structures
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    11. 2-11. Using Tuples
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  13. Chapter 3: Collections
    1. 3-1. Creating an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 3-2. Counting the Number of Items in an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 3-3. Managing Items in an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 3-4. Searching for Items in an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 3-5. Sorting an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 3-6. Replacing a Range of Values in an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 3-7. Iterating Over an Array
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 3-8. Saving an Array to the File System
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 3-9. Populating an Array with the Contents of a plist File
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    10. 3-10. Using Subscripts
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    11. 3-11. Creating a Dictionary
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    12. 3-12. Managing Items in a Dictionary
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    13. 3-13. Implementing the Hashable Protocol
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    14. 3-14. Iterating Through Items in a Dictionary
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    15. 3-15. Saving a Dictionary to the File System
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    16. 3-16. Populating a Dictionary with the Contents of a Property List File
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  14. Chapter 4: Advanced Swift Programming
    1. 4-1. Writing Closures
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 4-2. Writing Trailing Closures
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 4-3. Overloading the Equality Operator
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 4-4. Checking for Reference Equality
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 4-5. Implementing Generic Functions
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 4-6. Implementing Generic Classes
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 4-7. Working with Local Dates and Times
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 4-8. Creating a Unit Test Project
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 4-9. Writing a Unit Test
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    10. 4-10. Performance Testing with XCTest
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    11. 4-11. Creating Mock Objects for Testing
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    12. 4-12. Testing Asynchronous Code
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  15. Chapter 5: iOS Applications
    1. 5-1. Creating a New iOS Application
      1. Problem
      2. Solution
      3. How It Works
    2. 5-2. Adding a UILabel to a View
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 5-3. Adding a UIButton to a View
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 5-4. Adding a UITextField to a View
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 5-5. Positioning UIViews in Auto Layout Using NSConstraints
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 5-6. Repositioning a View to Accommodate the Keyboard
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 5-7. Displaying an Alert with UIAlertController
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 5-8. Using UIAlertController to Collect User Input
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 5-9. Creating a UITableView
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    10. 5-10. Swiping to Delete an Item from a UITableView
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  16. Chapter 6: OS X Applications
    1. 6-1. Creating an OS X Application
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 6-2. Adding a View to a Window
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 6-3. Adding a Menu and Menu Items
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 6-4. Adding a Button to a Window
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 6-5. Using an NSTextField
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 6-6. Displaying an Image in a Window
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 6-7. Adjusting Contents When a Window Is Resized
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 6-8. Implementing an NSTableView
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 6-9. Sorting an NSTableView
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    10. 6-10. Handling the Selection of an NSTableView Row
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  17. Chapter 7: Files and Directories
    1. 7-1. Locating Specialized Directories
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 7-2. Checking for the Existence of a File or Directory
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 7-3. Copying Files
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 7-4. Creating Directories
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 7-5. Deleting Files and Directories
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 7-6. Getting a List of Files from a Path
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 7-7. Archiving Objects to Files
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 7-8. Archiving Custom Classes to Files
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  18. Chapter 8: Concurrency
    1. 8-1. Threading with NSThread
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 8-2. Synchronizing Threads
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 8-3. Using Grand Central Dispatch for Threading
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 8-4. Using NSOperations and NSOperationsQueue
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 8-5. Completing Tasks in the Background in iOS
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 8-6. Downloading Content in the Background
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 8-7. Creating Long-Running Background Tasks
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  19. Chapter 9: Web Services
    1. 9-1. Parsing JSON
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 9-2. Parsing XML
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 9-3. Making HTTP Calls
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 9-4. Checking for Network Connectivity
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 9-5. Calling a REST API
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 9-6. Posting Data to a REST API
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  20. Chapter 10: Core Data
    1. 10-1. Creating a Data Model
      1. Problem
      2. Solution
      3. How It Works
    2. 10-2. Creating Model Classes
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 10-3. Creating a Data Store
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 10-4. Creating a Managed Object Context
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 10-5. Adding a New Entity
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    6. 10-6. Creating an NSFetchRequest
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    7. 10-7. Populating a UITableView with a Fetched Results Controller
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    8. 10-8. Deleting an Item
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    9. 10-9. Searching for Entities
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  21. Chapter 11: Advanced iOS 8 Features
    1. 11-1. Creating a Today Extension
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    2. 11-2. Creating a Custom Keyboard Extension
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    3. 11-3. Creating a Sharing Extension
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    4. 11-4. Creating an Action Extension
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
    5. 11-5. Creating a WatchKit Application
      1. Problem
      2. Solution
      3. How It Works
      4. The Code and Usage
  22. Index