Xcode Playground运行一直显示‘Running’无输出,新手求助排查问题
Hey there! As someone who’s been there with finicky Playgrounds, let’s walk through the most likely reasons your project’s stuck on "Running" and not displaying any UI, especially since you’re working from that older (but awesome) blog post:
1. You haven’t set up the Live View properly
Playgrounds don’t automatically show UI content—you need to tell it to! Here’s what to check:
- First, make sure your Playground is targeting iOS: Click the File Inspector (right sidebar, top icon that looks like a page), scroll to Playground Settings, and set Platform to iOS.
- Then, enable the Live View: Look for the little eye icon in the bottom-right corner of the editor and click it. This opens the panel where your UI should appear.
2. Your code isn’t linking to the Playground’s live view container
Even if you’ve built all the custom views/ViewControllers from the blog, Playground has no idea which one to display unless you explicitly set it. You’ll need to import the PlaygroundSupport framework and hook up your UI:
import PlaygroundSupport import UIKit // Replace this with your custom view or ViewController from the blog let mainView = YourCustomView(frame: CGRect(x: 0, y: 0, width: 320, height: 480)) // OR if you're using a ViewController: // let vc = YourCustomViewController() // vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) // This is the magic line that tells Playground to show your UI PlaygroundPage.current.liveView = mainView // PlaygroundPage.current.liveView = vc
If the blog post didn’t include this step (common in older posts), that’s probably the main issue!
3. There’s a hidden infinite loop or main thread block
Double-check your code for any unintended infinite loops (like a while true without a break condition) or synchronous blocking operations (like a network call run directly on the main thread). These will lock up the Playground and keep it stuck on "Running".
4. Xcode’s cache is acting up
Sometimes Xcode’s temporary files get corrupted, causing weird Playground behavior. Try these quick fixes:
- Go to Product > Clean Build Folder (hold Option to see this if "Clean" is there instead)
- Quit and restart Xcode entirely
- If that still doesn’t work, delete the Derived Data folder: Go to Xcode > Settings > Locations, click the arrow next to Derived Data, and delete the folder for your Playground.
5. Outdated API issues (since the blog is from 2015)
Even if you fixed obvious errors, some older Swift/UIKit APIs from 2015 are now deprecated or work differently. For example:
UIGraphicsBeginImageContextshould be replaced withUIGraphicsImageRenderer- Some Swift syntax (like function parameters, optional handling) has changed over versions
- Check for any red error indicators you might have missed, or use Xcode’s Fix-It suggestions (click the yellow/red dots) to update deprecated code.
Hopefully one of these fixes gets your UI showing up! Playgrounds can be quirky at first, but once you get the hang of the setup, they’re great for experimenting.
内容的提问来源于stack exchange,提问作者Parzival

