Playgrounds - APPENDICES - Understanding Swift Programming: Swift 2 (2015)

Understanding Swift Programming: Swift 2 (2015)

PART 5: APPENDICES

B. Playgrounds

A Playground is a part of Xcode (the interactive development envioronment for Swift and Objective-C) that presents a window and allows you to enter a sequence of Swift statements in a column at the far left of the window. The statements are executed and the results displayed in a second column to the right, known as the "results sidebar".

To run a Playground, you need to have Xcode installed on a Macintosh. The latest public version of Xcode can be obtained, for free, from the Apple Mac App Store. (It is fairly large, about 2.6 Gigabytes for Xcode 6.4.)

Once Xcode is installed, when it starts up it will display a window that will ask you if you want to do various things. One option is to run a Playground.

(This startup window is usually turned off by users, and if it has been turned off you can start a Playground by going to the File menu of Xcode at the top of your Macintosh screen and selecting New, then Playground.)

When you indicate that you want a Playground, you'll get a popup window that asks for a name (the default it provides is MyPlayground, which is fine) and asks you to choose your desired platform, iOS or OS X. Then click on Next.

You'll get another popup window displaying a part of your file system to indicate where the Playground will be saved. The likely default, Desktop, is fine. Click on Create.

A window like that shown below will then be displayed:

It will already be populated with two lines of code: an import statement, importing UIKit, which provides access to some useful libraries, and an example statement declaring a variable and setting a string to it, "Hello, playground".

This allows you to see the results that Playground displays in the right-hand column. Shown is the result of evaluating the part of the statement that is to the right of the assignment operator.

In the figure below I have entered some additional Swift statements, and you can see more of Playground's evaluation of results. Simple assignments to variables are evaluated and displayed immediately. The definition of a class, however, produces no results. If an instance is created of that class, however, the values of any properties defined in the class will be evaluated (e.g., the value of the property skinColor is shown as "red").

A third column, known as the "Assistant Editor" view, can be added if desired. This view shows what is normally seen in the console log.

Any print statements will have their output displayed in the third column, as console output, as shown in the figure below.

To display this third column, go to the Xcode menu at the top of your Mac screen and select the View menu, then Standard Editor, then Show Standard Editor. You may have to adjust the relative widths of the columns by dragging them to get a reasonable display.

Another Playground shows another interesting capability, known as the “value history” option:

A while loop is defined, with the variable e starting at 0 and then being incremented, with the loop ending when the variable e is 8.

The figure will indicate that execution passed through the loop 8 times. If you move the mouse cursor over the line with the e++, it will become shaded in gray as shown, and a circle with a "+" character will appear at the far right.

If you click on the "+" character, you will see the following:

This is known as the "value history" option, and graphically displays the value of the variable over time. (The x coordinate is time, while the y coordinate shows the value.)

This works for any expression that is evaluated while being repeated. Thus, the expression shown in the code in the figure below generates the display of a sine wave:

Rich Text Markup in Comments

This capability is sometimes described as a feature of Swift 2 (and Xcode 7), but is available in Xcode version 6.3 and later.

Comments can be marked up with rich text in a Playground. This isn't shown in the normal display of a Playground. The Editor menu in Xcode has an item (at the bottom) that can be toggled between "Show Raw Markup" and "Show Rendered Markup". The raw markup is just the commands, such as:

//: Playground - noun: a place where people can play

The rendered markup version looks like this:

To make use of markup, a colon character (":") is added to the characters indicating the beginning of a comment. This is done when the comment is a single line comment using two forward slashes:

//: This is a single-line comment

which is rendered as:

The same syntax (not /* */) is used for a multi-line comment:

//: This is a multi-line

//: comment and is displayed

//: as a block comment

The Playground markup can also display three levels of headers.

//: # This is a high level header

which is rendered like this:

This is a second-level header:

//: ## This is a second-level header

which is rendered like this:

This is a third-level header:

//: ### This is a third-level header

which is rendered like this:

Strings can be displayed in italics and in bold:

//: The word *italics* is displayed in italics.

//: The word **bold** is displayed in bold.

It is also possible to put links to different pages of a Playground and navigate between them.

The markup syntax also allows block quotes, bullet lists, numbered lists, blocks of code, horizontal lines ("horizontal rule"), inline elements, images, and special characters.

For details, see the Apple documentation.

Playgrounds have been used for purposes far beyond that of debugging code, with some people writing books with it. See the 2015 WWDC video “Authoring Rich Playgrounds”, session 405. For a transcript go to http://asciiwwdc.com/2015/sessions/405.

This particular syntax for displaying information, which is similar to but simpler than (more "lightweight") than HTML, is called "markup" by Apple but is widely known as "markdown" elsewhere. The scheme was originated by John Gruber.