How to Create a Playground in Swift - dummies

How to Create a Playground in Swift

By Jesse Feiler

Playgrounds are part of Xcode. You can use and reuse a single playground as a scratchpad or you can create a variety of unique playgrounds to test various features of your app. The choice is yours.

Keep your playgrounds in a Playgrounds folder on your desktop. At the root level of this folder keep a few playgrounds lying around. Use the convention of adding “Junk” to the name of any playground or file you don’t want to keep, and you can always find a few “Junk” playgrounds in that folder.

Within Playgrounds, also keep a folder or two for current projects. Playgrounds are just scratchpads and are more related to the computer you’re using than to the project you’re working on. Place playgrounds where they won’t get stored in Git, and this suggests the desktop folder. However, this is just a suggestion. You should use whatever is easiest for you.

Here are the steps to create a playground:

  1. Click File→New→Playground.

    The sheet shown here appears.


  2. Name your playground and choose iOS or OS X as the platform.

  3. On the next screen, choose the location for your playground.

    As noted earlier, you can place your playgrounds in a standard location (like my Playgrounds folder) or you can place them within your specific project folders (or anywhere else you like).

    That’s all there is to creating a playground. The figure shows a playground created for iOS. Note that if you have created a playground for OS X, the only difference is that it will import Cocoa rather than UIKit.

    The basic playground that you create has a comment at the top and a single import statement. Following that is a line of Swift code:

    var str = "Hello, playground"

    Note that in the panel to the right of the code, the value of str appears. This is the value as it was set in the main body of the playground.