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 2 By Example

Book Description

Create robust and extensible iOS apps using the advanced features of Swift 2

About This Book

  • Get up to speed with the new features of Swift 2 by following the exhaustive examples in this book
  • Specialize in developing real iOS apps, and 2D and 3D videogames using Swift and Cocoapods
  • Learn how to build server API apps to feed your iOS client apps

Who This Book Is For

This book is ideal for those who want to learn to develop app in Swift, starting the right way. Whether you are an expert Objective-C programmer or are new to this platform, you’ll quickly grasp the code of real world apps, and discover how to use Swift effectively. Prior experience in development for Apple devices would be helpful, but is not mandatory.

What You Will Learn

  • Create a server in Swift to deliver JSON data to an iOS app
  • Take advantage of Cocoapods to use third-party libraries
  • Use a clean and effective architecture to decrease complexity and speed up development
  • Take advantage of the most useful parts of the iOS SDK
  • Build games with SpriteKit and SceneKit
  • Develop an app running on the cloud to act as an API server for your client’s apps

In Detail

Swift is no longer the unripe language it was when launched by Apple at WWDC14, now it’s a powerful and ready-for-production programming language that has empowered most new released apps.

Swift is a user-friendly language with a smooth learning curve; it is safe, robust, and really flexible.

Swift 2 is more powerful than ever; it introduces new ways to solve old problems, more robust error handling, and a new programming paradigm that favours composition over inheritance.

Swift 2 by Example is a fast-paced, practical guide to help you learn how to develop iOS apps using Swift. Through the development of seven different iOS apps and one server app, you’ll find out how to use either the right feature of the language or the right tool to solve a given problem.

We begin by introducing you to the latest features of Swift 2, further kick-starting your app development journey by building a guessing game app, followed by a memory game. It doesn’t end there, with a few more apps in store for you: a to-do list, a beautiful weather app, two games: Flappy Swift and Cube Runner, and finally an ecommerce app to top everything off.

By the end of the book, you’ll be able to build well-designed apps, effectively use AutoLayout, develop videogames, and build server apps.

Style and approach

These easy-to-follow tutorials show you how to build real-world apps. The difficulty and complexity level increases chapter by chapter. Each chapter is dedicated to build a new app, beginning from a basic and unstyled app through to a full 3D game. The last two chapters show you how to build a complete client-server ecommerce app right from scratch.

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 By Example
    1. Table of Contents
    2. Swift 2 By Example
    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. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Welcome to the World of Swift
      1. The first look at Swift
        1. Let's go to the playground
        2. The building blocks – variables and constants
        3. Collecting variables in containers
        4. Controlling the flow
        5. Transforming the values using functions
        6. Structs – custom compound types
        7. Classes – common behavior objects
        8. Loose coupling with protocols
        9. Composing objects using protocol extensions
        10. Checking the existence of an optional value
        11. Enumerations on steroids
        12. Extended pattern matching
        13. Catching errors
        14. Swift functional programming patterns
      2. Summary
    9. 2. Building a Guess the Number App
      1. The app is…
        1. Building a skeleton app
        2. Adding the graphics components
        3. Connecting the dots
        4. Adding the code
      2. Summary
    10. 3. A Memory Game in Swift
      1. The app is…
      2. Building the skeleton of the app
      3. The menu screen
        1. Implementing the basic menu screen
        2. Creating a nice menu screen
      4. The game screen
        1. The structure
        2. Adding a collection view
        3. Sizing the components
      5. Connecting the dataSource and the delegate
      6. Implementing a deck of cards
        1. What we are expecting
        2. The card entity
        3. Crafting the deck
        4. Shuffling the deck
        5. Finishing the deck
        6. Put the cards on the table
        7. Adding the assets
        8. The CardCell structure
        9. Handling touches
      7. Finishing the game
        1. Implementing the game logic
        2. We got a pair
        3. We made the wrong move
        4. Et voilà! The game is completed
      8. Summary
    11. 4. A TodoList App in Swift
      1. The app is…
      2. Building a skeleton app
        1. Implementing an empty app
        2. Adding third-party libraries with CocoaPods
        3. Implementing the Todos view controller
      3. Building the Todos screen
        1. Adding entities
        2. Implementing datastore
        3. Connecting datastore and View Controller
        4. Configuring tableView
        5. Finishing touches
        6. Swipe that cell!
      4. Adding a Todo task
        1. The add a Todo view
        2. The add a Todo View Controller
        3. Finishing TodoDatastore
        4. List View Controller
        5. Where do we go from here?
      5. Summary
    12. 5. A Pretty Weather App
      1. The app is…
      2. Building the skeleton
        1. Creating the project
        2. Adding assets
      3. Implementing the UI
        1. The UI in blocks
      4. Completing the UI
        1. Implementing CurrentWeatherView
        2. Building WeatherHourlyForecastView
        3. Seeing the next day's forecast in WeatherDaysForecastView
      5. Blurring the background
      6. Downloading the background image
        1. Searching in Flickr
      7. Geolocalising the app
        1. Using Core Location
      8. Retrieving the actual forecast
        1. Getting the forecast from OpenWeatherMap
        2. Rendering CurrentWeatherView
        3. Rendering WeatherHourlyForecastView
        4. Rendering WeatherDaysForecastView
      9. Connecting to the server
      10. Where do we go from here?
      11. Summary
    13. 6. Flappy Swift
      1. The app is…
      2. Building the skeleton of the app
        1. Creating the project
        2. Implementing the menu
      3. A stage for a bird
        1. SpriteKit in a nutshell
        2. Explaining the code
        3. Simulating a three-dimensional world using parallax
        4. How to implement scrolling
      4. A flying bird
        1. Adding the Bird node
        2. Making the bird flap
      5. Pipes!
        1. Implementing the pipes node
      6. Making the components interact
        1. Setting up the collision-detection engine
      7. Completing the game
        1. Colliding with pipes
        2. Adding the score
        3. Adding a restart pop-up
      8. Summary
    14. 7. Polishing Flappy Swift
      1. Adding juiciness
        1. Let there be sounds!
        2. Playing the soundtrack
        3. Shaking the screen!
      2. Integrating with Game Center
        1. What Game Center provides
        2. Setting up Game Center
        3. Creating an app record on iTunes Connect
        4. Enabling Game Center
        5. Creating fake user accounts to test Game Center
        6. Authenticating a player
      3. Summary
    15. 8. Cube Runner
      1. The app is…
      2. Introduction to SceneKit
        1. What is SceneKit?
        2. Building an empty scene
        3. Adding a green torus
        4. Let there be light!
        5. Let's make it move!
      3. Implementing Cube Runner
        1. The game skeleton
        2. Implementing the menu
      4. Flying in a 3D world
        1. Setting up a scene
        2. Adding a fighter
        3. Texturing the world
        4. Make it move
        5. Adding cubes
        6. Adding more obstacles
      5. Adding a few touches
        1. The score
        2. Let's add music
      6. Summary
    16. 9. Completing Cube Runner
      1. Creating a real game
        1. Detecting collisions
        2. Game over!
        3. Adding the juice
        4. Game Center
      2. Summary
    17. 10. ASAP – an E-commerce App in Swift
      1. The app is…
        1. The first requirement: login and registration
        2. The second requirement: the products grid
        3. The third requirement: the open cart
      2. The skeleton app and register screen
        1. The skeleton app
      3. The ASAP e-commerce store
        1. The e-commerce product list
        2. The product cell
        3. Parsing and storing products
      4. The ASAP cart
        1. Adding a product to the cart
        2. Removing items from cart and checkout
      5. Summary
    18. 11. ASAPServer, a Server in Swift
      1. The interface of the ASAP Server
      2. One skeleton server for two OSes
        1. An OS X skeleton server
          1. Preparing the OS X environment
          2. The HelloWorld skeleton server
          3. Preparing the Linux environment
      3. The ASAPServer
        1. The Products
        2. The cart
        3. The order
      4. Connecting the ASAP app
        1. The products
        2. The Cart
        3. The order
      5. Summary
    19. Index