问题
I've diligently followed the Apple instructions to import a custom module into a playground, including the instructions here. And yet I get:
Playground execution failed: /var/folders/z3/kd0nj4ln1rgcpm8bdz7067wh0000gs/T/./lldb/1874/playground1.swift:7:8: error: no such module 'Foo' import Foo
How can I recover to a working Playground import? [Edit: Note, two answers have produced detailed instructions to associate a playground with a framework; I have followed those instructions but no luck. The solution will need to involve reconfiguring something in Xcode; my installation is apparently broken]
Detailed images of the error and attempts:
Here is another attempt, based on the answer of @EricD, showing the directory structure, no dice.
回答1:
For some of those that none of the above solutions work (and that Xcode build path setting was already set to Unique
), I've found a solution.
The framework must be built with a scheme for an iOS simulator device (any in the list) and NOT a Generic iOS Device
, as Playgrounds do not support it. This one worked for me :
回答2:
Here's how I proceed for OS X with Xcode 7.1:
Create new project: OS X Cocoa Framework, Swift. For this example I named it "TestPlaygroundFMK".
Create a new Swift file.
Add a class to the file. The code has to be public. I made this for our example:
import Foundation
public class Talk {
public class func sayHello() {
print("Hello from framework!")
}
}
Build the project now.
In the menu bar, click on File > Save As Workspace.
Create a new Playground and save it inside the project folder next to the Swift files.
The Playground must not have the same name as the project.
Build the project again.
Import your framework in the Playground and use it:
import TestPlaygroundFMK
Talk.sayHello()
回答3:
Create a workspace as below:
- Choose File > New > Workspace
- Enter the workspace name, and specify its location in your file system
- Click Save
Now, Create a Cocoa framework with swift file which have your class
- Choose File > New > Project
- Select Cocoa framework > Next
- Enter Product Name and Language > Next
- Before clicking create, in 'Add to:' and 'Group' list choose 'Your workspace name' as shown
- Framework is added to your workspace
- Create swift file in your framework
- In Xcode right click on the folder where you want to add swift file and select new file...
- Select swift > Next > Name your file > Create
- Select your framework in Xcode -> Build your framework
- Create playground -> Playground should also be part of your workspace, if not drag it to your workspace, it'll be added automatically.
- Right click in Xcode's files section > New file... > Playground
- import
- Call your method
Note: If you just wanted to add classes instead of framework to your playground check the link How to import my own class into an Xcode 7 playground?
回答4:
I had my playground working fine but found that when I quit Xcode and re-opened it, I would get an error like this:
error: /var/folders/ft/bmk8wh6s5ms4my2pxhn3qbp40000gn/T/playground1-9216e0..swift:3:8: error: no such module 'Playground_Sources' import Playground_Sources
... where "Playground" is the name of my playground file.
The solution to this (in Xcode 9.0) is to:
- click any playground file in your workspace
- show the Utilities panel (
opt
-cmd
-0
) - under "Playground Settings" change the "Platform" to anything else then set it back.
In my case, it's set to macOS
. I changed it to iOS
then back to macOS
(no recompilation necessary) and it started to work fine... that is until I close Xcode and open it again.
This is the only solution I found to work with macOS playgrounds. This bug still exists in Xcode 9.3.
回答5:
The solution, from Apple Support, was to adjust my Xcode Preferences. On Locations :: Advanced my configuration was 'Legacy'. By changing to 'Unique', and undoing any paths I'd attempted to insert, Playgrounds can now import frameworks.
回答6:
Solution that worked for me:
- Setup your workspace and project/framework.
- Delete derived data (How to remove derived data?)
- DO NOT BUILD YOUR PROJECT YET
- Create playground file with different name then your project/target/framework name under folder with either Swift files or where .xcodeproj is located (does not matter which one you chose)
- Build your project under required platform (if playground is for macOS, then build project with macOS target, etc.)
- When build finishes try adding some code to your playground and run it.
P.S. you might need to restart Xcode before 6th step.
回答7:
I don't think it's a stretch to say that Xcode is somewhat of a blackbox. I make no claim that this will work for anyone else, but for me, after having the same issue after creating a new playground and a new framework target, and all the other proposed solutions, what finally worked was deleting the workspace file and (in my case) regenerating it with cocoapods.
I have no idea why this worked.
回答8:

回答9:
Insert a var at the first line and re run
var str = ""
来源:https://stackoverflow.com/questions/33144877/playground-import-no-such-module-foo