Bound to the solution if TL;DR

Ever stuck in 'Launching Simulator' or 'Running Playground' when y'all fire up Playground to write some lawmaking? Despite restarting Xcode or fifty-fifty Mac multiple times, the trouble still persists? I accept seen many beginners give upwardly on learning Swift because of this issue and this really sadden me, Apple should do meliorate on this. Apple recommends beginner to utilise Playground and nonetheless Playground is full of problems like this 🤦‍♂️.

Launching

Running

I ran into this consequence last week while trying out NSPredicate examples in Playground, it happened after an error during execution of code.

Error in playground

After the error appeared, no large deal, I changed the code and run it again, and then the Playground got stuck in Running... 😱. Then I quit Xcode and open the Playground again, and the aforementioned issue persist 😭.

I tried restarting Xcode, Mac to no avail and about attempt to reinstall Xcode. Then I noticed it stuck on "Launching Simulator" and idea it might take something to do with the iOS Simulator since restarting Xcode doesn't help 🤔.

When y'all create a Playground in Xcode, it volition use iOS platform to run the code by default. To run it on iOS platform, the Playground volition spin up an iOS Simulator in the groundwork.

Platform iOS

And it is this iOS Simulator thing that causes the stuck problems! Restarting Xcode doesn't work because the simulator procedure doesn't get restarted when you restart Xcode equally it is a split up process.

Core Simulator

Somehow the CoreSimulator process become unstable after an error got raised in the Playground. It will become stuck after the fault and remain stuck until yous kill this process. That'due south the reason why you keep stuck on "Launching Simulator" or "Running.." even after restarting Xcode.

Workaround Solution

The solution for this is to switch to macOS platform in the Playground, let the playground code run in your macOS environment directly instead of spinning up an iOS simulator process to run information technology.

First, ensure your Playground doesn't utilize any UIKit function (eg: UIView etc), and remove the top import UIKit line if information technology is there.

So in the correct pane of the Playground window, select 'macOS' in Playground Settings -> Platform .

select macOS platform

I too suggest switching to manually run instead of automatic run in the playground to reduce the CPU usage.

Now yous should exist able to run the Playground successfully, cheers! 🍻

The caveat of this solution is that yous have to remove import UIKit and all UIKit related functions as macOS doesn't support UIKit.

Workaround Solution if you lot need to use UIKit

If you are using UIKit functions in Playground, you have to utilise iOS Platform every bit UIKit is iOS-exclusive.

The solution for solving the stuck issue then volition be to quit Xcode, open 'Activity Monitor' (In Spotlight, type 'Activity Monitor'), search for 'simulator' ,highlight com.apple tree.CoreSimulator , and forcefulness quit it.

quit simulator process

And so open Xcode again , information technology should work fine now. If you got whatever error during code execution in Playground, you will need to echo these steps. I recommend switching to manually run in Playground so that the Playground won't motorcar run the incomplete code while you are typing to foreclose occurrence of error.