Wednesday, October 5, 2016

How to use the Swift Playgrounds iPad app to code on the go



Image: Apple
Swift Playgrounds is an easy way to learn Swift programming and to test code inside of Xcode, and now the feature is available for iOS 10. Learn how to use the Swift Playgrounds app for iPad.

By Cory Bohon | October 4, 2016, 8:13 AM PST


Apple announced Swift Playgrounds earlier this year during WWDC '16, andreleased 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.


More about Mobility
8 reasons why professionals should get the iPhone 7
Report: Your business is wasting money on BYOD reimbursements
iOS 10 and the enterprise
Subscribe to TechRepublic's Mobile Enterprise newsletter

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).

Figure A
Image: Cory Bohon/TechRepublic



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.

Figure B
Image: Cory Bohon/TechRepublic

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...