Mobile Development

Playgrounds

Playgrounds are a fantastic way to learn programming in Swift. They allow you to quickly experiment and test sections of code without having to build and compile to a device or simulator.

Creating a new Playground with Xcode

We will go through the quick process of opening a new Playground and start editing some Swift code:

  1. Open Xcode
  2. Select "New Playground" or select "File" -> "New" -> "Playground"
  3. Create a name for your Playground and save it to disk
  4. Update code in the left window to see it update on the right!

It's really simple to get started and there are powerful things you can do. Use Playgrounds to test out new code, experiment with someone else's code and isolate problem areas from your existing projects.

Let's see how we can get started in Playgrounds with an example project.

Example

Following the steps above, open a new Playgrounds project. Copy and paste the following code into your source code view:

import UIKit

let currentTimeAndDate = NSDate()
var currentTemperature = 72.0
println("Current temperature oustide is \(currentTemperature)")

let oneHourFromNow = NSDate(timeIntervalSinceNow: 3600)
currentTemperature = 74.0
println("Current temperature oustide is \(currentTemperature)")

let twoHoursFromNow = NSDate(timeIntervalSinceNow: 6400)
currentTemperature = 76.0
println("Current temperature oustide is \(currentTemperature)")

let threeHoursFromNow = NSDate(timeIntervalSinceNow: 9600)
currentTemperature = 80.0
println("Current temperature oustide is \(currentTemperature)")

The above code will update a temperature variable based on new times throughout the day. In later chapters we will go over the syntax of Swift, but for now, we are just looking at Playgrounds from a tool perspective.

Your playgrounds file should now look like the below. In our diagram, you can see

  1. Where source code is written and modified
  2. Where the current output of code is shown
  3. Where you can view specific objects in detail
  4. Eyeball - selecting the eyeball will show detail about that specific line of output. Use this to quickly see detail about the current variable.
  5. Assistance pin - Use this circle to pin a new window into the assistance editor. This is helpful for viewing changes to a particular variable over time.

The majority of our examples in this Gitbook will be shown in Playgrounds. It's a good idea to start getting more comfortable now. Part of this process will be watching Apple introduce Playgrounds at WWDC 2014.

[Video] Swift Playgrounds

Apple introduce Playgrounds as part of the new Xcode 6 at this year's WWDC event. Below is an overview to getting started with Playgrounds straight from the horses mouth at this year's WWDC event. What's new in Xcode Explore how Playgrounds provide new workflows, enable rapid development, help you conveniently step through your code to diagnose bugs, and make it easier than ever to learn new concepts. (Apple WWDC Session 408)

References

Exploring and Evaluating Swift Code in a Playground

Experiment with Swift code in an interactive environment known as a playground.

https://developer.apple.com/library/ios/recipes/Playground_Help/Chapters/AboutPlaygrounds.html#//apple_ref/doc/uid/TP40015166-CH28-SW1