You are previewing Swift 2 Cookbook.
O'Reilly logo
Swift 2 Cookbook

Book Description

Over 50 hands-on recipes to help you create apps and build your portfolio of projects in Swift 2

About This Book

  • Conceptualize and write effective applications for iOS/OS X using Swift 2

  • Make the most of Swift’s diverse features, from configuring your projects to using design patterns and different types of databases

  • A fast-paced, comprehensive guide equipped with lots of tips and tricks to help you learn about Swift programming quickly

  • Who This Book Is For

    If you are an Apple developer who wants to use Swift to build your applications, this book is for you. With Swift 2 going open source, this book will also help web developers. Some understanding of Objective-C or Swift would be helpful but is not required.

    What You Will Learn

  • Install Xcode from the App Store

  • Upgrade your existing Swift 1.2 code to Swift 2.0

  • Use the standard Swift 2.0 library and collections

  • Work with structs, generics, and design patterns in Swift

  • Use the new markup language to provide rich documentation of code

  • Write a book using Playgrounds and Swift

  • Debug and test your code using Xcode

  • Integrate your code with Objective-C and use assembly code with Swift

  • Leverage the Objective-C runtime when compiling Swift using associated objects

  • In Detail

    Swift is Apple’s innovative development language that was introduced at the WWDC (WorldWide Developers Conference) 2014 alongside Xcode 6 and iOS 8. This user-friendly language is packed with modern features to make programming easier and fun, with a lot of flexibility. With Swift 2 comes even better performance, a new error handling API, protocol extensions, and super support for availability checking.

    This book will equip you with all the practical programming aspects of Swift 2. It covers the important features and paradigms that Swift provides to developers. You will begin by installing Xcode from the App Store and using Swift as an interpreter. Then, you will see how you can upgrade your existing Swift 1.2 code to Swift 2.0 with the help of Xcode. You will see how to use structs and generics, and work with different Design Patterns with Swift. Debug and test your code using Xcode and see how to use assembly code effectively with Swift.

    By the end of this book, you will have the necessary skills to get the most out of Swift to develop effective mobile and web applications.

    Style and approach

    This book follows a problem-solution methodology using step-by-step recipes, and is filled with screenshots to help you create samples and learn something new in every recipe.

    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 2 Cookbook
      1. Table of Contents
      2. Swift 2 Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why Subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Xcode and Swift
        1. Introduction
        2. Installing Xcode from the App Store
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Downloading the Xcode image
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Starting a Swift project
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Using the Swift project options
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Creating conditional code
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Migrating an existing project to Swift 2.0
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Adding a developer account
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Compiling from the command line
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        10. Using Swift as an interpreter
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        11. Adding a control version system to an existing project
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      9. 2. Standard Library and Collections
        1. Introduction
        2. Creating HTML manually
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more...
        3. Printing your object description
          1. How to do it…
          2. How it works...
          3. There's more...
        4. Quizzing the user
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. There's more…
        5. Searching for perfect numbers
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Sorting an array of products
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more...
        7. Finding the way out
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Creating your own set
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Organizing a dinner room
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more…
      10. 3. Using Structs and Generics
        1. Introduction
        2. Creating an exam app
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Checking for the right answer
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Avoiding the copying of structs
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Creating a generic array initializer
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Creating a priority list
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Creating a protocol for the priority queue
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      11. 4. Design Patterns with Swift
        1. Introduction
        2. Writing into a log file
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Creating a factory of musical notes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Simulating a home automation
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Delivering some pizzas
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Protocol-oriented programming
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      12. 5. Multitasking in Your App
        1. Introduction
        2. Word counting using threads
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Creating an SEO app
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Creating a CycloneChecker app
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Checking links of our website
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      13. 6. Working with Playgrounds
        1. Introduction
        2. Creating the first playground
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Watching some graphics
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Watching a temperature with color
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Stretching an image
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Beautifying your text using Pangrams
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Receiving JSONs
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Creating our own class representations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Rich documentation
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        10. Navigating pages in playgrounds
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      14. 7. Swift Debugging with Xcode
        1. Introduction
        2. Validating the values – developing a tax income simulator app
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Debugging with Xcode and Swift – the best checker movement
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Debugging with LLDB
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Profiling an app
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Error handling in Swift 2.0
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Custom error handling in Swift 2.0
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Availability checking in Swift 2.0
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also…
      15. 8. Integrating with Objective-C
        1. Introduction
        2. Calling a cab
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Hiring a van
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Porting your code from one language to another
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Replacing the user interface classes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Upgrading the app delegate
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Creating your own custom framework
          1. Getting ready
          2. How to do it…
          3. How it works…
      16. 9. Dealing with Other Languages
        1. Introduction
        2. Using my old address book
          1. Getting ready
          2. How to do it…
          3. How it works...
        3. Compressing a message
          1. Getting ready
          2. How to do it…
          3. How it works…
            1. There's more…
        4. Using assembly code with Swift
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Sharing C++ code with Swift
          1. Getting ready
          2. How to do it…
          3. How it works…
      17. 10. Data Access
        1. Introduction
        2. Creating an SQLite database
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Checking where your IP is from
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Tracking your phone activity
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Controlling your stocks
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Designing a voting device using CouchDB
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      18. 11. Extensions, Photos, and More
        1. Introduction
        2. Developing the geekiest keyboard
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Time to take your pill
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Adding effects to your photos
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Being a film critic
          1. Getting ready
          2. How to do it…
            1. Creating the workspace
            2. Developing the iOS part of the app
            3. Coding the class
            4. Testing the app
          3. How it works…
          4. There's more…
          5. See also
        6. Leaving breadcrumbs
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Creating the Currency Converter app
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Method swizzling in Swift
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Associated objects in Swift
          1. Getting ready
          2. How to do it…
          3. How it works…
      19. Index