Introducing the iOS Playground
For a new developer, getting started with a language can be a pain. You’ve got to figure out how to create new projects, understand how a bunch of development tools work, and if you’re lucky, after a few hours you might get “Hello World” to display on your screen.
When Apple introduced Swift in Xcode 6, they realized that developers would need a way to get their feet wet (or hands dirty, if you prefer) without all the steps of creating new iOS applications. Heck, why would you want to try building an application if you aren’t sure you’re even going to be writing code that works? Therefore, the iOS Playground was born. The Playground gives you an area to type in experimental code and see the result—immediately—without even pressing a Run button.
Creating a New Playground
As a first step, you’ll need to create a new Playground. We’ll be using the Playground throughout the book, so understanding this process is a must.
To create a new Playground, choose File, New, Playground from the Xcode menu, as shown in Figure 3.2.
FIGURE 3.2 Create a new Playground from the Xcode File, New menu.
When prompted, provide a name for the playground and make sure that the platform is set to iOS, as demonstrated in Figure 3.3. The name can be anything you’d like. Unlike a project, a Playground creates a single file, so it’s easy to rename later. Click Next to continue.
FIGURE 3.3 Name the playground and set the Platform to iOS.
Finally, choose where the Playground will be saved, and then click Create. After a few seconds, the Playground window opens, as shown in Figure 3.4.
FIGURE 3.4 The new playground opens, already populated with some sample code.
Using the Playground
When the Playground first opens, it already contains some sample code that imports the UIKit framework and defines a variable named str. You can safely delete the str line if you’d like, but I recommend that you leave the import statement at the top. This adds access to most of the objects and methods you’ll need to actually do anything useful in the playground.
So, what do you do now? You play. Any code that you enter is immediately evaluated and the results appear in the margin on the right.
For example, remember the calculation of a person’s age in days? Try typing this code into the playground:
var
userAge:Int
=30
var
userAgeInDays:Int
userAgeInDays = userAge *365
As you type the lines, watch what happens in the margin on the right. For the first statement, declaring userAge, you’ll see 30 appear in the margin because the variable contains the value 30. The second line won’t generate any output because it doesn’t contain anything yet. The third line, however, calculates and stores the age in days, which is displayed in the margin (10,950, if you’re interested). You can see this in Figure 3.5.
FIGURE 3.5 The contents of your variables are shown on the right.
Generating and Inspecting Output
The iOS Playground can be used to generate and display output as well as inspect variables. Later in the book, we’ll use it to examine the contents of web pages retrieved by our code (and in a variety of other exercises). A simple way to generate output in Swift, however, is to use the print(<String>) or println(<String>) functions. These take a string as an argument and print it—with println adding return at the end.
Add these lines to the end of the code in the Playground:
for var
count =0
; count <50
; count++ {println
("Count is
\(count)"
) }
Shortly after typing the lines, you’ll see a value in the margin quickly count to 50. This is a count of the number of times the println statement was executed. What you don’t see, however, is the output that is generated.
To view the output, position your cursor over the line that reads “(50 times).” You’ll notice two icons appear; the first (an outline of an eye) is the Quick Look icon and lets you inspect some variable contents more closely. (We’ll do this in the next hour.) The second icon, a circle, opens the assistant editor and displays a value history for that item, as well as any output that was generated.
Go ahead and click the circle by the println statement. The assistant editor displays the output from the println statement, as well as a draggable timeline at the bottom. If your code has values that change over time, you can drag the timeline to inspect their value at an arbitrary point in time. These controls are visible in Figure 3.6.
FIGURE 3.6 Additional output and controls are available in the assistant editor.
The beauty of the Playground is that you can do anything you want. Go back and try some of the Swift syntax discussed in this hour. Try comparing dates, test a few loops and switch statements... whatever you want.
We’ll be using the Playground often to explore new code concepts, even making it do a few things that might surprise you. I recommend using it now to gain experience writing Swift.