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.

Image for post
Image for post
Photo by Bambi Corro on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

Recommended: Be able to produce a “Hello, World!” Swift application (guide HERE)

Terminology

Playgrounds: A development environment that can be used to create Swift code

Swift: An open source programming language for macOS, iOS, watchOS and tvOS

Playground basics

You can download Playgrounds from HERE which installs the App onto your device.

We can then writelet str = "Hello, playground")

followed by

print("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.

Image for post
Image for post

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

Image for post
Image for post

You’ll need to choose a destination for your Playground as you go. Here is a visual guide:

Image for post
Image for post

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:

Image for post
Image for post

The console / debug panel allows us access to the debugger, which is something that is extremely useful.

Inspecting Runtime values

As shown above, you can click on “abc” on the right-hand side of the screen.

Image for post
Image for post

This can allow you to Add a view that can display the runtime value.

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 \n

Image for post
Image for post

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:

Image for post
Image for post

Accessing files from GitHub in Playgrounds

Simply download the file from GitHub, and double-click on the Playgrounds file. Not too tricky!

  1. if the file is just text copy and paste the code into Swift playgrounds — it is that easy!
  2. If available, click on the download link for the Playground and click copy to Playgrounds

Not too tough!

Conclusion…

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:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store