Enter code in Swift Playgrounds on Mac
In the Swift Playgrounds app, you enter code in the coding area on the left side of the screen. The easiest way to add code is to click a code suggestion in the shortcuts list below the coding area.
If you want to add code that’s not in the shortcuts list, you can enter your own code or choose from a list of commonly used code elements.
Adjust the view
Most playgrounds have a coding area (where you enter code) and a live view, where you run your code to see the result. If you want more room to code, you can hide the live view, or if you only want to see the result, you can hide the coding area.
In a playground in the Swift Playgrounds app on your Mac, expand either the coding area or the live view to full screen by dragging the border between them.
To see the coding area when it’s hidden, choose View > Show Editor.
To see the live view when it’s hidden, choose View > Show Live View.
Use the shortcuts list
To see code suggestions based on the code in your playground, click in the coding area. The code suggestions appear in the shortcuts list below the coding area. You can click a shortcut to quickly insert it in your code at the cursor.
To use the shortcuts list in the Swift Playgrounds app on your Mac, click in the coding area to place the insertion point where you want to add the code.
Click a code suggestion in the shortcuts list.
The code appears in the playground at the cursor. If the code requires a variable name or value, these are highlighted in your code so you can quickly type to replace them.
Note: To compress the shortcuts list to make more room for your code, click .
See a list of code elements
While you’re coding, you can open a list of commonly used code elements and add them to your code.
In the Swift Playgrounds app on your Mac, click in the coding area of your playground to place the insertion point where you want to add code, then click above the coding area.
Click to see commonly used code elements, then click a code element to insert it.
Use instructions and code examples
Some playgrounds have instructions and code examples on the right side of the screen covering the live view. To page through the instructions, swipe through the text or click the dots or the arrows in the navigation bar at the top.
In the Swift Playgrounds app on your Mac, to use a code example, drag it into your coding area.
As you drag, the insertion point appears where the sample code is being placed.
When the insertion point is where you want the new code, lift your finger.
To try the code example and see what it does, click Run My Code at the bottom of the live view.
If there are instructions on the right side of the screen, the instructions slide down when you click Run My Code so you can watch the code run. When you click Stop, the instructions slide back up.
Add images and files
You can add images and files to a playground. For example, if you want to use a custom background image in a coding project, you can use one of the photos from your photo library.
In a playground in the Swift Playgrounds app on your Mac, click near the top right.
Do any of the following:
Add an image: Click , then select an image. You can also click Insert From to select an image from iCloud Drive or the Finder, or click Photos to select a photo in your photo library.
Add a file: Click , then select a file. You can also click Insert From to select a file from iCloud Drive or the Finder.