Apple announced Swift Playgrounds earlier this year during WWDC ’16, and released it alongside iOS 10 in September. This new app is great for beginner coders and professional developers, because it allows you to officially write Swift code and have it run on the iPad. This is a first for iOS–Apple has never allowed apps that can compile or interpret code.

Perhaps the biggest feature of the Swift Playgrounds app for iOS is the ability to download tutorial projects to learn Swift; these projects are pretty self-explanatory so I won’t cover them. I’ll walk you through the workings of Swift Playgrounds, and how to write own code inside the app.

SEE: Apple’s Swift programming language: The smart person’s guide

What is a Playground?

Swift is Apple’s newest programming language, and it can be used to write apps on all of its platforms: iOS, macOS, tvOS, and watchOS.

Playgrounds are a feature of Swift and the Xcode IDE that allows for quick compiles in a REPL (read-eval-print-loop) style interface, mimicking the REPL found in run-time interpreted languages like Ruby. Playgrounds are simple files that contain Swift code and can be compiled and run quickly, providing output logging to the user as the code is executed.

For example, if you write a for loop that iterates over an array to add new items, then for each iteration of this for loop, the playground will show the index of the loop and the output of the code executed in that loop. In this scenario, it would show each new item added to the array during the for loop execution. This is a nice feature that can help you understand exactly what the code is doing, and provide visual output along the way.

Apple has been involved with getting Swift Playgrounds into schools through the Everyone Can Code initiative, and it’s a very good app for teaching kids and adults how to develop in Swift.

Downloading Swift Playgrounds

The Swift Playgrounds app is a free download from the App Store. It’s currently only available for iPad models capable of running iOS 10.

Using Swift Playgrounds with new code

To start a new coding project in Swift Playgrounds, follow these steps.

  1. Open the Swift Playgrounds app on iPad.
  2. Select the + button at the top left of the screen.
  3. Select Create Playground.
  4. Choose a template from the available options Answers, Blank, Graphing, and Shapes. (We’ll use a Blank template.)
  5. Start writing Swift code in the code editor.

While you’re writing code, the autocorrect toolbar at the bottom of the screen will offer code completion suggestions similar to how the Xcode editor works (Figure A).

Figure A

Swift Playgrounds code editor showing the code completion toolbar that sits above the virtual keyboard, or on the hardware keyboard options.

When you’re done, tap the Run My Code button to compile and execute the Swift code (or if you have a hardware keyboard connected, press Command + R, the same as in Xcode, to run). If there are errors, red dots will appear beside the lines with errors, along with compiler warnings about what is potentially wrong.

Importing existing Playgrounds from Xcode

Importing Playgrounds from Xcode can be nice when you want to try your hand at coding on the go. To do this, you have two simple options.

  1. Store your documents in iCloud Drive. This is the easiest method and lets you access your iCloud Drive documents by tapping the + icon in Swift Playgrounds and selecting iCloud Drive or your other favorite cloud document storage solution, e.g., Dropbox.
  2. Email your documents. Once you’ve emailed a Swift document to yourself, open the email on the iPad, and tap the Playground file. You’ll be prompted to select a share location–select the Copy To Playgrounds option.

Recording screen for demos

You now have the ability to record the entire Swift Playgrounds screen while you type, demonstrate items, and more. If you’re using Swift Playgrounds to share tutorials on how to write Swift code, this feature is for you.

To begin a new recording, follow these steps.

  1. Open the Swift Playgrounds app on iPad.
  2. Create a new Playground or select an existing custom Playground.
  3. Tap the More button (the three dots icon).
  4. Tap Record Movie.
  5. At the top of the screen, a Record button will appear; tap the red Record button to start recording and to capture the screen.

When finished, tap the Record button again to stop the recording process. After a few seconds, a video will be created and displayed. In this window (Figure B), you can play back the video. When finished, tap the Save button to save the completed video to the Photos app.

Figure B

The Save button inside of the playback viewer to instantly save the video to the Photos app. Or, tap the Share button for other sharing options.