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, 2nd Edition

Book Description

Get valuable hands-on experience with Swift 3, the latest version of Apple’s programming language. With this practical guide, skilled programmers with little or no knowledge of Apple development will learn how to code with Swift 3 by developing three complete, tightly linked versions of the Notes application for the OS X, iOS, and watchOS platforms.

In the process, you’ll learn Swift’s fundamentals, including its syntax and features, along with the basics of the Cocoa, CocoaTouch, and WatchKit frameworks. This book teaches you how to use common design patterns for Swift, how to structure an application for Apple’s platforms, and how to submit working apps to the App Store.

Divided into four distinct parts, this book includes:

  • Swift 2 basics: Learn Swift’s basic building blocks and features for object-oriented development
  • OS X app development: Set up the document model, build out features, and sync data with iCloud
  • iOS app development: Use multimedia, contacts, location, notifications, and iCloud files to build a fully featured iOS Notes app
  • Advanced app extensions: Build an Apple Watch app, and learn how to debug, monitor, and test all three of your Swift apps

Table of Contents

  1. Preface
    1. Resources Used in This Book
    2. Audience and Approach
    3. Organization of This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Safari
    7. How to Contact Us
    8. Acknowledgments
  2. I. Swift Basics
  3. 1. Getting Started
    1. The Apple Developer Program
      1. Registering for the Apple Developer Program
      2. Downloading Xcode
    2. Creating Your First Project with Xcode
      1. The Xcode Interface
    3. Developing a Simple Swift Application
      1. Designing the Interface
      2. Connecting the Code
    4. Using the iOS Simulator
    5. Conclusion
  4. 2. The Basics of Swift
    1. The Swift Programming Language
      1. Swift 2 Versus Swift 3
    2. Playgrounds
    3. Comments
    4. Variables and Constants
    5. Operators
    6. Control Flow
      1. Loops
      2. Switches
    7. Types
      1. Working with Strings
      2. Comparing Strings
      3. Searching Strings
      4. Optional Types
      5. Type Casting
      6. Tuples
      7. Arrays
      8. Dictionaries
      9. Enumerations
      10. Associated Values
      11. Sets
    8. Functions and Closures
      1. Using Functions as Variables
      2. Closures
      3. The defer Keyword
      4. The guard Keyword
    9. Making Your Code Swifty
    10. Conclusion
  5. 3. Swift for Object-Oriented App Development
    1. Classes and Objects
      1. Initialization and Deinitialization
      2. Properties
      3. Inheritance
      4. Protocols
      5. Extensions
      6. Access Control
      7. Operator Overloading
      8. Generics
      9. Subscripts
    2. Structures
    3. Modules
    4. The Swift Standard Library, Foundation, Cocoa, and Cocoa Touch
    5. Swift Package Manager
    6. Data
      1. Loading Data from Files and URLs
      2. Serialization and Deserialization
    7. Error Handling
    8. Memory Management
    9. Design Patterns in Cocoa and Cocoa Touch
      1. Model-View-Controller
      2. Delegation
    10. Structuring an App
      1. The Application Delegate
      2. Window Controllers and View Controllers
      3. Nibs and Storyboards
    11. Conclusion
  6. II. A macOS App
  7. 4. Setting Up the macOS Notes App
    1. Designing the macOS Notes App
    2. Creating the macOS Project
    3. Defining a Document Type
    4. Adding the Icon
    5. Conclusion
  8. 5. Working with Documents on macOS
    1. The NSDocument Class
    2. Storing Data in the Document
    3. Storing Text
      1. Package File Formats
      2. The guard Keyword, and Why It’s Great
      3. Saving Files
      4. Loading Files
    4. A Basic UI
    5. Conclusion
  9. 6. User Interfaces and iCloud
    1. Updating the UI
    2. Document-Filetype-Extension UI
      1. Getting an Icon for the Collection View Cells
      2. Adding Attachments
      3. Storing and Managing Attachments
      4. Displaying Data in the Collection View
    3. Enhancing Attachments
      1. Opening Attachments
    4. Adding Attachments via Drag-and-Drop
      1. Adding QuickLook
    5. Location
    6. iCloud
    7. The Basics of iCloud
    8. Conclusion
  10. III. An iOS App
  11. 7. Setting Up the iOS Notes App
    1. Designing the iOS Notes App
    2. Creating the iOS Project
    3. Enabling the iOS App for iCloud
    4. Defining a Document Type
    5. Conclusion
  12. 8. Working with Files in iCloud
    1. The App Sandbox
    2. iCloud Availability
    3. Creating the Document List View Controller
      1. View Controllers and Storyboards
      2. The Navigation Controller
      3. Collection Views
      4. Using Constraints to Control Size and Position
    4. Creating the Document Class
    5. Listing Documents
    6. Creating Documents
    7. Downloading from iCloud
    8. Deleting Documents
    9. Renaming Documents
    10. Conclusion
  13. 9. Working with Documents on iOS
    1. Adding a View to Display Notes
    2. Editing and Saving Documents
    3. Conclusion
  14. 10. Working with Files and File Types
    1. Setting Up the Interface for Attachments
    2. Listing Attachments
      1. Determining Types of Attachments
      2. Displaying Attachment Cells
    3. Dealing with Conflicts
    4. Creating the Quick Look Thumbnail
    5. Conclusion
  15. 11. Images and Deletion
    1. Adding Attachments
    2. Adding Image Attachments
    3. Viewing Attachments
    4. Deleting Attachments
    5. Conclusion
  16. 12. Supporting the iOS Ecosystem
    1. Sharing with UIActivityController
    2. Handoffs
    3. Searchability
      1. Indexing Activities
      2. Spotlight Extensions
    4. Conclusion
  17. 13. Multimedia and Location Attachments
    1. Audio Attachments
    2. Video Attachments
    3. Location Attachment
    4. Conclusion
  18. 14. Polishing the iOS App
    1. Opening Links in SFSafariViewController
      1. 3D Touch
      2. Home Screen Quick Actions
      3. Peek and Pop
    2. Settings
    3. Undo Support
    4. Images with Filters
    5. Worldwide Apps
      1. Internationalization
      2. Localization
    6. Accessibility
    7. Splitscreen Multitasking
    8. Conclusion
  19. IV. Extending Your Apps
  20. 15. Building a watchOS App
    1. Designing for the Watch
    2. Designing Our watchOS App
    3. Creating the watchOS Extension
      1. Communicating with the iPhone
      2. User Interfaces for the Apple Watch
      3. Showing Note Contents
      4. Creating New Notes
      5. Adding Handoff Between the Watch and the iPhone
      6. Glances
    4. Conclusion
  21. 16. Code Quality and Distribution
    1. Debugging
    2. Instruments
    3. Testing
      1. Unit Testing
      2. UI Testing
    4. Using Objective-C and Swift in the Same Project
      1. Using Swift Objects in Objective-C
      2. Using Objective-C Objects in Swift
    5. The App Store
      1. App Thinning
      2. Testing iOS Apps with TestFlight
    6. Conclusion
  22. Index