62

The Playground in Xcode automatically updates as you type, but I can't figure out how to get the Playground to "re-compile".

In many cases this wouldn't matter, but if you're writing code that generates or uses random values it can be useful to run it a few times to make sure it's working. Is there any way to get the playground to reset / refresh / re-run?

Seen a few questions asking how to stop the Playground from auto-updating, but nothing for the opposite.

Easiest way to do this seems to be to just edit the code (add and remove a space), or put in some kind of a loop... Just wondering if there's a menu shortcut etc.

shim
  • 9,289
  • 12
  • 69
  • 108

7 Answers7

62

Try Editor > Execute Playground from Xcode menu

enter image description here

I don't know what Reset Playground is, by the way.

shim
  • 9,289
  • 12
  • 69
  • 108
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • 3
    The "Reset playground" can be used, by some playgrounds, to reset to its original state. I.e. In the GuidedTour.playground from the swift book, you can change the examples, and then afterwards reset everything by running "Reset playground". – holroy Feb 25 '15 at 22:04
  • 2
    "Reset playground" appears to be gone (I'm using Xcode 8.3). Below "Execute Playground" it is now "Show Rendered Markup", and searching for "Reset Playground" in the "Help" menu doesn't have any results. – shim Mar 31 '17 at 15:45
  • 3
    In Xcode 10 it is now "Run Playground" and as [Gagandeep's answer](https://stackoverflow.com/a/53531975/1032372) indicates, it has a keyboard shortcut too. – shim Nov 29 '18 at 17:16
45

In addition to the other responses, playground execution can also be controlled with the button on the bottom bar above the debugger console. There are two buttons on the left side of the console; the one on the left opens/closes the console and the one on the right controls the execution of the playground and changes depending on whether the playground is currently running.

If you hover over the button a small black triangle will appear in the bottom corner of the button. If you long press on the button the menu in the screenshot below will appear, where you can change the execution mode of the playground between automatically or manually running. If it's automatic, then it should run once every time you make a change (unless you make a change while it's running, then it will interrupt itself and run again). If it's manual then you have to press the button to run the playground.

playground menu

While running the button is a hollow square, like a stop button on a VCR. Pressing it will stop the execution.

stop

When it's not running it is a blue triangle, like a play button on a VCR. Pressing it will run the playground.

play

(Running Xcode 8.3)

shim
  • 9,289
  • 12
  • 69
  • 108
37

Also, you can create your shortcuts.

Xcode > Preferences > Key Binding > (Search Execute Playground)

It is empty as a default, but you can assign anything.

Xcode 11 > Preferences > Key Binding > (Run Playground)

andreskwan
  • 1,152
  • 1
  • 13
  • 13
Cem Bego
  • 371
  • 3
  • 3
25

In Xcode 10.1 there is a "Run Playground" command in the "Editor" menu which can be accessed via the following keyboard shortcut:

Command + Shift + Enter

screenshot of menu showing Run Playground

shim
  • 9,289
  • 12
  • 69
  • 108
Gagandeep Gambhir
  • 4,225
  • 1
  • 29
  • 34
10

in Xcode 10, just use command(⌘) + shift(⬆︎) + Enter(↲)

shim
  • 9,289
  • 12
  • 69
  • 108
Mor4eza
  • 265
  • 3
  • 12
9

As in Xcode version 10.2 the "Execute Playground" command in the "Editor" menu has been replaced by "Run Playground" and has a shortcut by default + SHIFT + ENTER

The reason why it doesn't refresh when you enter the same command again, is because Playground is already running. You first need to stop the Playground by either clicking on the stop button or entering your own shortcut as I did.

To do so go in

Xcode > Preferences > Key Binding

and look for "Stop Playground".

screenshot

Add the shortcut you would like to use clicking in the empty field on the right. I entered:

+SHIFT + >

In this way you can use the default shortcut to run the playground and you use the custom shortcut to stop it. Once stopped you can run it again.

Here in the menu:

screenshot

multitudes
  • 2,898
  • 2
  • 22
  • 29
6

Same in Xcode 11: Cmd-Shift-Return

HenryRootTwo
  • 2,572
  • 1
  • 27
  • 27