Enter code in a playground book in Swift Playgrounds on iPad
In a playground book, you enter code in the coding area on the left side of the screen. Some of the playground books in the top section on the More Playgrounds screen suggest commands in a 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 using the keyboard, and even use sample code that’s in preexisting code modules. (See Work with shared files.)
As you type code, suggestions for completing your code may appear. To accept a suggestion, tap it.
Adjust the view
Most playground books 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 book in the Swift Playgrounds app on your iPad, touch and hold the resize handle between the coding area and the live view, then drag the handle to expand one or the other to full screen.
Use code suggestions in the shortcuts list or the shortcuts bar
In some playground books in Swift Playgrounds, you can use the code suggestions in the shortcuts list (or shortcuts bar) to quickly insert commands, delete code, or add a new line. For example, you’ll find the shortcuts below the coding area in the playground Get Started with Code, located in the top section of the More Playgrounds screen.
The shortcuts bar shows controls and code suggestions that are based on the code in your playground book. To see these suggestions, tap in the coding area.
To use the shortcuts bar in the Swift Playgrounds app on your iPad, tap in the coding area to place the insertion point where you want to add code.
Tap a code suggestion in the shortcuts bar.
The code appears in the coding area. If the code requires a variable name or value, the keyboard opens.
Use the keyboard to enter the characters you need, then tap to close the keyboard.
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 iPad, tap in the coding area of your playground book to place the insertion point where you want to add code, then tap above the coding area.
Tap at the top to see commonly used code elements, then tap a code element to insert it.
Enter characters using the keyboard
You can enter code—including letters, numbers, and special characters—using the keyboard.
In the Swift Playgrounds app on your iPad, tap in the coding area to open the shortcuts bar.
Tap in the shortcuts bar to open the keyboard, then do any of the following:
Add a letter: Tap a letter key.
Add a number or symbol: Swipe down on a letter key until it changes to the number or symbol you want to enter.
For example, if you want to enter the number 5, swipe down on the T key so it changes to a 5.
To instantly bring up the keyboard, double-tap in the coding area.
Use instructions and code examples
Some playground books 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 tap the dots or the arrows in the navigation bar at the top.
In the Swift Playgrounds app on your iPad, to use a code example, first touch and hold it until it lifts up, then drag it into your coding area.
As you drag, the insertion point appears in your code where the sample code will be inserted.
When the insertion point is where you want the new code, lift your finger.
To try the example code and see what it does, tap 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 tap Run My Code so you can see the code run. When you tap Stop, the instructions slide back up.
Add images and files
You can add images and files to a playground book. 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 iPad, tap near the top right of the coding area.
Do any of the following:
Add an image: Tap , then select an image. You can also tap Insert From (at the bottom of the library) to select an image from iCloud Drive or the Files app, or tap Photos to select a photo from the Photos app.
Add a file: Tap , then select a file. You can also tap Insert From (at the bottom of the library) to select a file from iCloud Drive.