Coding in Swift Playgrounds
Whether you are using a MacOS or iOS device this is great!
Playgrounds are great! You can use them on a Mac to quickly run code, and even on the iPhone or iPad! Fantatastic.
You’ll find Playgrounds in Interactive courses, and even downloads from GitHub (here is a guide if you are uncomfortable with this), and indeed there is even a link at the bottom of this post go the Playground created during this tutorial.
Difficulty: Beginner | Easy | Normal | Challenging
Playgrounds: A development environment that can be used to create Swift code
Swift: An open source programming language for macOS, iOS, watchOS and tvOS
Creating a Playground: On an iOS device
You can download Playgrounds from HERE which installs the App onto your device.
We can then write
let str = "Hello, playground")
and then press the Run My Code button.
In order to get the output to display I clicked the “abc” at the right-hand side of the screen. This then gives the option of Adding a view — and this displays the result of the command.
Creating a Playground: On a Mac
You do need to have downloaded and installed Xcode on your machine.
You can then go through the menu File > New Playground
I then choose iOS > New Playground in the following screen. By choosing iOS we get UIKit imported for us (which is great, as we need that).
You’ll need to choose a destination for your Playground as you go. Here is a visual guide:
You’ll have seen from that video, when I run
var str = "Hello, playground") print("Hello, playground") console/debug panel appears at the bottom of the screen. Which brings us to a good opportunity to highlight the main areas of the screen:
The main areas of the Mac Playgrounds screen:
The console / debug panel allows us access to the debugger, which is something that is extremely useful.
Inspecting Runtime values
On an iPad
As shown above, you can click on “abc” on the right-hand side of the screen.
This can allow you to Add a view that can display the runtime value.
On a Mac
On the right hand side of the screen we can see the runtime value of the Strings — note that the final one shows the next line character
Even better — run your mouse over the “Hello, Playground” and a small eye icon pops up. This allows you to view all the elements in an array, or similar. In this case, it displays the String in a new window:
Accessing files from GitHub in Playgrounds
On the Mac
Simply download the file from GitHub, and double-click on the Playgrounds file. Not too tricky!
On an iOS device
- if the file is just text copy and paste the code into Swift playgrounds — it is that easy!
- If available, click on the download link for the Playground and click copy to Playgrounds
Not too tough!
Importing your own classes and frameworks into playgrounds for Mac: https://stackoverflow.com/questions/24045245/how-to-import-own-classes-from-your-own-project-into-a-playground/24049021#24049021
Playgrounds are a great way of experimenting with code. If you only have an iOS device while you’re out and about don’t let it stop you from trying to solve a current coding problem — just get out your device and code!
Want to get in contact? Try the link here: