Run code in a playground in Swift Playgrounds on Mac
When you’re ready to see the result of your code, you need to run it. If your playground has a live view, it can reflect the result of running your code. In some playgrounds, you can have result icons appear to the right of your code as it runs, and then click them to see the value of your result.
If your code has a print statement, you can view its output in the Console by clicking after you run your code.
If you want to visualize the flow of your running code or debug it, you can have Swift Playgrounds highlight each line of code as it runs, helping you identify where problems occur.
Run code
In the Swift Playgrounds app on your Mac, click Run My Code (or use the Touch Bar).
If there are instructions on the right side of the screen, they slide down when you click Run My Code, so you can watch your code run in the live view. When you click Stop, the instructions slide back up.
To rerun it, click Run My Code again. (If it’s already running, click Stop, then click Run My Code.)
If your code doesn’t run and a colored dot appears in it, see If your code doesn’t run.
Highlight code or change the animation speed
When you run code in a playground in the Swift Playgrounds app on your Mac, you can do any of the following:
Highlight code as it runs: Click , then choose Step Through My Code.
To highlight your code at a slower pace, choose Step Slowly.
Change the animation speed in the live view: On some playground pages, you can run the code faster by clicking and then choosing Run Fastest or Run Faster.
To return to the default run speed, choose Run My Code.
See a result after running code
In some playgrounds in the Swift Playgrounds app on your Mac, result icons appear to the right of your code. The result icon changes based on the type of information in the result. For example, if you assign “Hello, playground” to a string variable and run your code, you see a result icon with “abc” on it.
Perform any of the following actions:
To see the value of the result, click the result icon.
To see the result as you work in the code, click the result icon, then click Add Viewer.
To remove the viewer, click the result icon again, then click “Remove viewer.”
To prevent the result from appearing, click , then turn off Enable Results.
View code feedback or suggestions
After you run code in some playgrounds, a message might appear, giving feedback or suggestions on what to do next.
To close the message when you’re finished with it, click . You can also click Next (or another button that appears) to move on. You can reopen the message by clicking .
Tip: If appears after you run code, you can click it to open the Console and see the output from any print statements in your code. If a dot appears on , it indicates that there’s new, unread output in the Console. To close the Console, click again. (Not all playgrounds show .)
If your code doesn’t run
If your code doesn’t run in Swift Playgrounds on your Mac, a colored dot appears next to the line of code that has an issue.
Click the dot to see an explanation of the issue and any suggestions for how to fix it.
To reset your playground after entering and running code, see Reset a playground.