Playground Import: No Such Module 'Foo'
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()
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 :
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.
I'm not sure i had the exact problem as the original question - but given the mix and age of solutions and the lack of this one that DID work for me, i thought I'd share.
In Xcode 11+ (tested this on 12.2), i'd often have Playgrounds fail to run givn the "No Such Module" error when it included a framework that is a part of the workspace.
The solution for me was in the "File Inspector" for the Playground, and the "Build Active Scheme" option:
This ensures the project was being built appropriately for the target, and now my annoying compile issues are gone!