What is Swift Playground and how to create?

Swift Playground

Tested on: Xcode 12.2, Swift 5.3

Playground allows you to quickly write and test code without creating an complete application. This is very useful if you want to test out your algorithm before going into your app project. In this Swift Playground Reference, the following topics will be cover :

  1. How to Create a Swift Playground?
  2. Playground Overview
  3. Turning on Line Number
  4. Code Execution (Run your Code)

Create a Swift Playground:

When you open Xcode, it will greet you with a welcome screen that looks like the image below. If you don't see this, probably you have unchecked Show this window when Xcode lauches. However, this screen it not important if you want to open Swift Playground.

Xcode Welcome Screen

To create a Swift Playground, go to File > New > Playground or type option + shift + command + N and a playground screen will appear for you to choose a template.

Playground Template Selection

The selection allows you to choose the type of template you want to create the playground. The options available are iOS, macOS, tvOS. Each platform come with it own environment.

In most cases and for the purpose of all the courses and reference in Appdevelopments.com, you will use iOS and choose a Blank template. Click next to continue. Xcode will ask you to name the playground and save it to an location.

Playground Overview

Playground Template Selection

Among all these toggle, the most important one are Activity View, Play / Pause Button, Result and Debug Area. You will understand the rest of the toggle, when you use playground more often.

  1. Activity View : Shows the status of the playground. In the image above, it shows Ready to continue means that is it ready to handle more execution. If there is a spinner shows in the activatity view, it means that the code is excuting or running.
  2. Show Navigator : The toggle open or close the navigator area.
  3. Navigator Area : From left to right, (Project navigator)This area allows you to access, add or delete files,(Source Control Navigator) showing the branches and all the local source control information, (Symbol navigator) browse the symbols in your project as a list, (Find navigator) search and filters options, (Issue navigator) view issues and (Report navigator) view the history of your build, run, debug, continuous integration, and source control tasks
  4. Show Inspector : The toggle open or close the inspector area.
  5. Play / Pause Button: This button allows you to run the entire playground or stop it. You can change the setting to Automatically Run by long pressing on it.
  6. Results Area : This shows the results of the code.
  7. Debug Area : This shows the print statement or the error message.

Turning on Line Number. You can turn on line number by going Xcode > Preferences > Text Editing > Line Numbers and check the line number box. Line number is useful when you want to refer to the part or line of the code.

Code Execution

Playground excute code from top to bottom and the play button at each line of the code enable you to run the code from the beginning to that line of code. Once Xcode execution is finished, the status is shown on the Activity viewer.

Moreover, you can use the Play / Pause Button to run the entire playground or stop it. You can change the setting to Automatically Run by long pressing on it.