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.
We will go through the quick process of opening a new Playground and start editing some Swift code:
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.
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
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.
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. 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)
Experiment with Swift code in an interactive environment known as a playground.