An Introduction to Xcode 12 Playgrounds
Get started with a simple Xcode playground, and see how Playgrounds provide the ideal environment to learn Swift programming.
What is a Playground?
A playground is an interactive environment where Swift code can be entered and executed with the results appearing in real-time. This makes an ideal environment in which to learn the syntax of Swift and the visual aspects of app development without the need to work continuously through the edit/compile/run/debug cycle that would ordinarily accompany a standard Xcode project. With support for rich text comments, playgrounds are also a good way to document code for future reference or as a training tool.
Creating a new Playground
To create a new Playground, start Xcode and select the “File -> New -> Playground…” menu option. Choose the iOS option on the resulting panel, and select the Blank template.
The Blank template is useful for trying out Swift coding. The Single View template, on the other hand, provides a view controller environment for trying out code that requires a user interface layout. The game and map templates provide preconfigured playgrounds that allow you to experiment with the iOS MapKit and SpriteKit frameworks, respectively.
On the next screen, name the playground LearnSwift
, and choose a suitable file system location to save the playground before clicking on the Create button.
Once the playground has been created, the following screen will appear ready for Swift code to be entered:
The panel on the left-hand side of the window (marked A in Figure 1-1) is the Navigator panel, which provides access to the folders and files that make up the playground. To hide and show this panel, click on the button indicated by the left-most arrow. The center panel (B) is the playground editor where the lines of Swift code are entered. The right-hand panel (C) is referred to as the results panel, and that is where the results of each Swift expression entered into the playground editor panel are displayed. The tab bar (D) will contain a tab for each file currently open within the playground editor. To switch to a different file, simply select the corresponding tab. To close an open file, hover the mouse pointer over the tab and click on the “X” button when it appears to the left of the file name.
The button marked by the right-most arrow in the above figure is used to hide and show the Inspectors panel (marked A in Figure 1-2 below) where a variety of properties relating to the playground may be configured. Clicking and dragging the bar (B) upward will display the Debug Area (C) where diagnostic output relating to the ...