173

Every time I create a new playground in order to test some code, Xcode gets stuck and won't run the code. It simply presents 'Running playground' or 'Launching simulator' statement at the top of the screen with the loading icon promisingly spinning next to it but nothing happens. Sometimes this continues indefinitely and sometimes Xcode halts and prints this to console :

Playground execution failed: error: Couldn't lookup symbols:
__swift_FORCE_LOAD_$_swiftCoreImage 
__swift_FORCE_LOAD_$_swiftFoundation 
_playground_log_hidden 
_playground_logger_initialize 
_playground_log_postprint 

thread #1: tid = 0xc0cd0, 0x000000010ea7c3c0 MyPlayground`executePlayground, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2

frame #0: 0x000000010ea7c3c0 MyPlayground`executePlayground
frame #1: 0x000000010ea7b9c0 MyPlayground`__37-[XCPAppDelegate enqueueRunLoopBlock]_block_invoke + 32 
frame #2: 0x000000010f59625c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
frame #3: 0x000000010f57b304 CoreFoundation`__CFRunLoopDoBlocks + 356
frame #4: 0x000000010f57aa75 CoreFoundation`__CFRunLoopRun + 901
frame #5: 0x000000010f57a494 CoreFoundation`CFRunLoopRunSpecific + 420
frame #6: 0x0000000114985a6f GraphicsServices`GSEventRunModal + 161
frame #7: 0x0000000110124f34 UIKit`UIApplicationMain + 159
frame #8: 0x000000010ea7b6e9 MyPlayground`main + 201
frame #9: 0x0000000112ad268d libdyld.dylib`start + 1
frame #10: 0x0000000112ad268d libdyld.dylib`start + 1

I am running Xcode 8.0 (8A218a) on macOS Sierra 10.12.

Hardware:
MacBook Pro (13" Mid-2012)
2,5 GHz Intel Core i5
4 GB 1600 MHz Ram DDR3

I have looked around but at least neither of these threads have provided an answer:
https://forums.developer.apple.com/thread/5902
https://github.com/jas/playground/issues/9

Things I have already tried with zero success:

  • Restarting Xcode
  • Reinstalling Xcode (downgraded to 7.3 but since that didn't help I upgraded back to 8.0)
  • Restarting the machine
  • Creating a new playground

Do you have any ideas on how to solve this problem? I am new to programming and eagerly trying to learn Swift but Xcode is making it practically impossible.

starball
  • 20,030
  • 7
  • 43
  • 238
Emil Osvald
  • 1,857
  • 2
  • 10
  • 8
  • 1
    Thank you for your quick reply. I am using playground simply because I actually have an active subscription at Treehouse.com and they encourage using playground when testing new concepts. – Emil Osvald Oct 11 '16 at 13:53
  • 1
    I understand, but my thought is, you are "eagerly trying to learn Swift" and right now you are stuck. If you can make a new app project and run it, you can proceed. Now, if you also can't run an app project, then you have deeper issues. :) – matt Oct 11 '16 at 14:09
  • 1
    That is definitely true. Thank you for your input matt, I'll do as you suggested as long as there is no better solution. I'll nevertheless still keep this thread active in case someone has figured out a solution. – Emil Osvald Oct 12 '16 at 12:53
  • 3
    I have no advice to give here, @EmilOsvald. I can report this happens frequently on my MacBook as well (and happened all the time when I used iMacs, too, all new machines with the latest updates). You should not feel at fault for wanting to use Playgrounds. In theory they are a great product and when they work they are. – Ilias Karim Jun 02 '17 at 20:47
  • 1
    There should be no spaces before the first comment (if the first line of text is a comment), nor an empty line as the first line. Let me know. –  Jun 04 '17 at 23:25

25 Answers25

232

Ans. Switch to macOS platform

When you create a Playground, by default it select iOS platform and automatically imports UIKit. It's the UIKit which crashes and stuck all the time. If your (practice) code does not use UIKit then you can switch to MacOS platform. As major (not exact though) difference between iOS and macOS is in APPKit and UIKit.

In the upper right corner of your Xcode Playground, click on show/hide Utilities--> file inspector --> Playground Setting--> Platform--> macOS

See screenShot below: enter image description here

PANKAJ VERMA
  • 3,450
  • 1
  • 16
  • 15
  • 5
    This seemed to work for me along with setting the playground execute button to manual. Will they ever fix this though, it just seems a bit trashy – hoboBob Apr 27 '18 at 23:06
  • 4
    Thanks it works good when using Foundation, exactly what I needed. I can't believe Playgrounds have so much bugs... – Adrien Yvon May 22 '18 at 16:32
  • 30
    Ridiculous. 3 lines of code (none of which imports UIKit) and I have to google this. A lot of people would just give up and declare Swift useless. – James Bedford Jul 16 '18 at 09:32
  • This helped me with a Playground that wouldn't work after restarting Xcode. It's not a complete fix though, as I've had Playgrounds freeze up with the platform set to macOS. A relaunch usually fixes it though. – Kal Aug 09 '18 at 02:17
  • 5
    This doesn't work for me. I'm using Xcode 9.4.1 (not the beta), and I have to continually restart Xcode. Pretty lame ... – Michael Rogers Sep 07 '18 at 21:31
  • This is very clever but alas it does not magically cause UIKit to become useful again: you can't switch back to iOS afterwards and have it run – matt Jan 03 '19 at 19:45
  • Didn't help in XCode 10.1. Still need to restart every freakin' few minutes. – Robo Robok Feb 07 '19 at 02:25
  • 1
    Also, if the playground gets big enough, Cmd+Shift+Enter stops working. Who the hell programmed this crap? Kids using Playgrounds on iPad? – Robo Robok Feb 07 '19 at 02:31
  • Don't forget to `import Foundation` to make the code work. – Nike Kov Apr 24 '20 at 08:52
  • Also along with the selecting the macOS as platform, please uncheck the other options "Render Documentation" and "build active scheme". I am using Xcode 13.0 Beta – priya Jul 29 '21 at 09:32
73

I have tried the following and it worked for me:

  1. press on the bottom "stop playground" button
  2. remove the first comment line and make import statement the first line. no space above import.
  3. press execute button
commanda
  • 4,841
  • 1
  • 25
  • 34
Aaeb
  • 1,276
  • 11
  • 10
  • 1
    I found that no space should be present before a comment either - but you _can_ have a comment as the first line. Just no empty line before it, nor indentation before it, on its line. –  Jun 04 '17 at 23:24
  • 3
    I would add to that the need to restart Xcode. It seems to work for me. – John Doe Jun 30 '17 at 23:20
  • 29
    Worked for me. I cannot believe how ridiculous this bug is. Apple should be ashamed. – gangelo Sep 15 '17 at 16:16
  • 2
    restarting xcode was the only thing that worked for me – George Filippakos May 03 '18 at 10:37
  • 1
    Awesome. I tried quitting xcode, clearing derived data etc. but nothing worked except this. Although I already had import at the first line! – Shivam Pokhriyal Jan 24 '19 at 14:05
  • So I didn't had any comment before the first import so what I did is I added it, run,stop, then remove it and run again and that fixed it. My guess is that xcode is somehow doing some lazy compiling for old rows so by editing the first line you edit the whole document and that makes the compiler start compiling all over again, like doing a "clean" – user2387149 Jun 10 '20 at 07:13
61

In my case, Xcode 8.3.2, it works when I try as steps below:

  1. File -> Close "Name.playground"
  2. File -> Close Playground
  3. Quit Xcode
  4. Re-open Xcode
Khoa Vo
  • 1,005
  • 10
  • 7
  • 2
    Offering something besides me too, does anyone using modern XCode have an insight on the root cause of this failure? This turns out to be the best non-invasive work around, however, I wouldn't consider anything short of problem resolution a solution. Do comments in the playground header really have any part in the problem? And what does Apple say in regards to the cause? – truedat101 Mar 06 '18 at 00:58
  • This worked for me along with removing the empty line before my print statement. – FromTheStix May 30 '18 at 14:11
  • 3
    Ug, what a crappy way to have to work around an IDE issue. – PruitIgoe Jun 20 '18 at 16:50
  • Agreed, this is super lame. – Ilias Karim Sep 02 '19 at 17:10
  • Im on Xcode 13.1 and this keeps happening I can't believe they haven't fixed it yet. – Santiago Alvarez Nov 24 '21 at 13:46
54

Buggy Simulator's main process is still working even after Xcode closing. So here're the steps:

  1. Quit Xcode;
  2. Kill com.apple.CoreSimulator.CoreSimulatorService process in Activity Monitor;
  3. Launch Xcode and run your Playground.
dbv
  • 677
  • 5
  • 5
22

In my experience, you should:

  1. Prevent Swift playground refresh, by holding: the ► arrow at the bottom left part of the Playground and selecting "Manually Run"
  2. Quit Xcode
  3. Restart Xcode

It works very well.

Cœur
  • 37,241
  • 25
  • 195
  • 267
grain
  • 329
  • 2
  • 4
17

Adding import PlaygroundSupport solved the problem for me :)

SiBC
  • 187
  • 3
  • 2
    Interesting. Where did you get this from? –  Jun 04 '17 at 23:22
  • If it's as effective as you say, you'd think the devs would have that included by default. I'll experiment with this. Thanks! – saphryn Jun 16 '18 at 04:01
16

I have cancelled the comment on the first line. It solved for me. Make sure to try it after you have closed and relaunched Xcode and force quit com.apple.coresimulator from Activity Monitor

Fiveagle
  • 1,005
  • 8
  • 11
  • 1
    On XCode 8.2.1, this worked for me. Once I deleted the first line comment about playgrounds, it ran quickly. I did not have to force quit the simulator. – jessi Mar 22 '17 at 01:08
  • Thanks to @Fiveagle answer. I had, indeed, to force qui the coresimulator process, remove the first lines so "import UIKit" is the first line. And restarting XCode made the tricks, but the first compilation took a while to perform. – yves Baumes Apr 25 '17 at 09:51
7

You can switch to manual processing. It worked for me. screenshot

Sandhu
  • 818
  • 9
  • 18
3

Make sure you kill xcode in the activity monitor before eventually killing com.apple.CoreSimulator.CoreSimulatorService process in the activity monitor. I tried killing the process a number of times and I realized if xcode is still running in the background activity then the com.apple.CoreSimulator.CoreSimulatorService process would pop up again even when you just killed it.

Asad Abbas
  • 55
  • 1
  • 7
2

I had the same problem, all that I did is to open Assistance editor and everything works fine with me even when I closed the Assistance editor! Don't ask why it was trial and error. Hope this will help you

Farisk
  • 83
  • 7
  • That's actually a great idea. Since the play/stop button doesn't agitate the process or force a refresh, I guess you can think of toggling the Assistant Editor on and off as a replacement for the play button lol. Awesome :)). – saphryn Jun 16 '18 at 04:04
2

After beating my head on this for a few hours, I realized I had accidentally deleted this, VERY MUCH important line that allows to test UIKit elements before committing my playground onto git:

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

Facepalm. Hopefully this can help someone from pulling their hair out for a few hours.

apunn
  • 97
  • 8
1

I'm using Xcode 10.2 and am getting the same issue. So generally just use Online Swift Playground

This serves my purposes! :-)

pkamb
  • 33,281
  • 23
  • 160
  • 191
DialDT
  • 422
  • 4
  • 6
1

In my case the problem was user-induced. I had the assistant editor displayed, but the live view wasn't selected. Make sure it looks like this:

LiveView

And not like this:

enter image description here

Mozahler
  • 4,958
  • 6
  • 36
  • 56
  • 1
    Thanks, this was indeed my issue. On XCode 14 now, and it seems to randomly close windows between compiles sometimes – blueether Nov 14 '20 at 01:05
1

For me, the issue was the fact that MyPlayground was syncing to iCloud. Xcode saves your files every few seconds, and it leads to "not responding" states.

So there is a simple solution- just go to:

System Preferences - Apple ID - Apps on this Mac using iCloud- iCloud Drive - Options- And then uncheck "Playground"

That little trick helps to solve frozen "Running".

Prevent Playgrounds from syncing

Vitya Shurapov
  • 2,200
  • 2
  • 27
  • 32
  • This answer is wrong. What you are suggesting affects the Playgrounds _app_ and has no effect on Xcode playgrounds (i.e. _.playground_ files) stored in iCloud Drive. – pius Jul 18 '21 at 11:07
  • Unchecking this flag somehow helped my Xcode playgrounds to get things done. Anyway in macOS Big Sur, there is no playgrounds option on iCloud Drive ;) – Vitya Shurapov Jul 19 '21 at 13:40
1

XCode 12.3 and still same issue happens.

Solution for me is Command + Q ( restart the Xcode )

zeytin
  • 5,545
  • 4
  • 14
  • 38
0

after runnning a while you will see error message then you go to view>debug area > activate console and then play again worked for me

Community
  • 1
  • 1
  • 1
    What is a while? 5 minutes? A day? –  Jun 04 '17 at 23:21
  • from 5 to 15 min maybe, after some weeks of this problem I changed to SSD and upgraded to 16GB RAM Memory and its solved, like day and night change – CrtlBreak Sep 27 '17 at 01:01
0

Check for iCloud storage plan!

If you do have one, it constantly syncs the entire computer which includes Desktop and Document folders. Instead try creating a "Local Files" folder in your Home folder(command+shift+h) and keep your Playground projects there, where it doesn't sync to iCloud.

This worked for me.I hope this works for you as well! :)

theNoobDev10
  • 409
  • 5
  • 6
0

I deleted derived data and clean build folder. This works for me. I was facing with Xcode 11.2.1 and Xcode 10.1 on same machine.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pawan Kumar
  • 508
  • 3
  • 19
0

Also happens, like in my case, when you've defined a function, but have forgotten to call it?

msimons
  • 495
  • 10
  • 19
0

As if this question didn't have enough answers, I will provide my "solution" to the stuck Swift playgrounds problem:

The one solution I've come across which works nicely for me (so I do not have to restart Xcode):

killall launchd_sim

This will cause the spinning "Running Playground..." to go away and you can trigger a new execution. Be aware: the next execution is going to spin up launchd_sim and all those child processes again. It would probably be best to kill only the relevant child process. But I didn't find it so far.

But I do this only when I'm not using the iOS Simulators in parallel, as the launchd_sim process is a child process of Simulator.

pd95
  • 1,999
  • 1
  • 20
  • 33
0

To speed up the build in Xcode Playground and prevent the loading icon to keep spinning forever:

  1. go to the sidebar on the right, and change iOS to macOS in Playground Settings
  2. instead of importing UIKit, import Foundation

(this would work if you're not trying to use specific stuff from tthe UIKit framework)

Kelvin Jou
  • 271
  • 2
  • 12
0

I got this answer from : Ioannis Diamantidis - How to fix Xcode Playground getting stuck on "Running". I am using Xcode version 13.2.1 with a Mac Mini M1. I went to the playground icon on the top right corner - Hide or show the Inspectors, it says Playground Settings , I select in Platform macOs. now my playground is running perfect, I see the results everywhere

-1

I was facing the same issue but after googling and experimenting a bit, I found out that one must always import UIKit before importing any other libs.

amagain
  • 2,042
  • 2
  • 20
  • 36
-1

Firstly, delete the inbuilt comment line.

Then, even if you put a first line comment back in, it still works. enter image description here

Harry McGovern
  • 517
  • 5
  • 19
-1

One last tip that works for me: Make sure no iOS simulator is running. They conflict with each other.

ahbou
  • 4,710
  • 23
  • 36