Enter code in a playground in Swift Playgrounds on Mac
In a playground, you enter code in the coding area on the left side of the screen. Some of the playgrounds in the top section of the More Playgrounds screen suggest commands in the shortcuts list (or shortcuts bar), 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.
You can also type code, use sample code that appears in a playground, and use code in preexisting code modules. See Work with shared files.
As you type code, suggestions for completing your code appear. To accept a suggestion, press Return.
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
In some playgrounds in Swift Playgrounds, you can click in the coding area to see code suggestions based on the code in your playground. The code suggestions appear in a shortcuts list (or shortcuts bar) 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 into a bar to make more room for your code, click .
Enter code elements from the library
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 example code 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 above the coding area.
Do any of the following:
Add an image: Click , then select an image. You can also click Insert From (at the bottom of the library) 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.