如何在Xcode 11 Playground中使用图片?SwiftUI教程图片不显示求助
Hey there, I’ve dealt with this exact headache while learning SwiftUI on Mojave with Xcode 11 Playgrounds—super frustrating when you’re following the Apple tutorial step-by-step and the image just won’t show up. Let’s go through the most common fixes that got my image working:
Double-check how you added the image to Resources
Playgrounds have a specific way to handle resources that’s easy to mess up. Do this instead of just dragging the file into the sidebar:- Press
Cmd + 1to open the Project Navigator. - Find your Playground’s
Resourcesfolder (it’s nested under the Playground file). - Right-click the folder and select Add Files to "[Your Playground Name]".
- Select
turtlerock.jpg, make sure Copy items if needed is checked, and click Add.
This ensures the image is properly bundled with the Playground instead of just sitting in the same folder.
- Press
Verify your image reference code
On Mojave’s Xcode 11 Playgrounds, the standardImage("turtlerock")sometimes doesn’t pick up the resource correctly. Try using this explicit approach instead:Image(uiImage: UIImage(named: "turtlerock")!)Also, triple-check the filename spelling and capitalization—Mojave’s file system might enforce case sensitivity here, so
turtlerock.jpgvsTurtleRock.jpgwill break things.Clear Xcode’s Derived Data cache
Playgrounds love to hold onto old cached resources. Clear it out:- Open Xcode, go to Xcode > Preferences > Locations.
- Next to "Derived Data", click the small arrow to open the folder.
- Delete the entire folder (it’ll rebuild automatically when you reopen Xcode).
- Restart Xcode and your Playground, then run the code again.
Confirm the image itself isn’t corrupted
Sometimes the issue is just a bad image file. Openturtlerock.jpgwith Preview to make sure it loads properly. If it doesn’t, re-download the image from the Apple tutorial and try again.Keep in mind Mojave’s SwiftUI limitations
Remember, SwiftUI was officially released with Catalina—Xcode 11 on Mojave only has partial support. If all else fails, try closing the Playground’s preview pane, clicking the Run My Code button at the top, then re-opening the preview. Sometimes forcing a fresh run fixes the rendering glitch.
Hope one of these gets your turtle rock showing up—happy SwiftUI learning!
内容的提问来源于stack exchange,提问作者Maric Vikike

