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.
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.
- Open the Swift Playgrounds app on iPad.
- Select the + button at the top left of the screen.
- Select Create Playground.
- Choose a template from the available options Answers, Blank, Graphing, and Shapes. (We’ll use a Blank template.)
- 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).
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.
- 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.
- 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.
- Open the Swift Playgrounds app on iPad.
- Create a new Playground or select an existing custom Playground.
- Tap the More button (the three dots icon).
- Tap Record Movie.
- 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.
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.
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays