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
操场上执行失败:/var/folders/z3/kd0nj4ln1rgcpm8bdz7067wh0000gs / T /。/ / 1874 / playground1 lldb。错误:没有这样的模块‘Foo’导入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]
如何恢复到可工作的游乐场入口?[编辑:注意,有两个答案给出了详细的说明,将操场与框架联系起来;我遵从了这些指示,但没有成功。解决方案将需要重新配置Xcode中的某些内容;我的装置显然坏了]
Detailed images of the error and attempts:
错误和尝试的详细图像:
Here is another attempt, based on the answer of @EricD, showing the directory structure, no dice.
这里是另一个尝试,基于@EricD的答案,显示了目录结构,no dice。
6 个解决方案
#1
21
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.
对于上述解决方案中任何一个都不起作用的一些解决方案(Xcode构建路径设置已经设置为Unique),我找到了一个解决方案。
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 :
这个框架必须建立在一个iOS模拟器设备(列表中的任何一个)的方案中,而不是一个通用的iOS设备,因为操场不支持它。这个对我很有用:
#2
13
Here's how I proceed for OS X with Xcode 7.1:
以下是我如何用Xcode 7.1操作OS X:
-
Create new project: OS X Cocoa Framework, Swift. For this example I named it "TestPlaygroundFMK".
创建新项目:OS X Cocoa框架,Swift。对于这个示例,我将它命名为“TestPlaygroundFMK”。
-
Create a new Swift file.
创建一个新的Swift文件。
-
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.
在菜单栏中,单击File > Save As Workspace。
-
Create a new Playground and save it inside the project folder next to the Swift files.
创建一个新的运动场,并将它保存在Swift文件旁边的项目文件夹中。
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
5
-
Create a workspace as below:
创建如下所示的工作区:
- Choose File > New > Workspace
- 选择File >新的>工作区
- 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
现在,使用包含类的swift文件创建一个Cocoa框架
- Choose File > New > Project
- 选择文件>新的>项目
- Select Cocoa framework > Next
- 接下来选择Cocoa framework >
- Enter Product Name and Language > Next
- 接下来输入产品名称和语言>
- Before clicking create, in 'Add to:' and 'Group' list choose 'Your workspace name' as shown
- 在单击create之前,在“Add to:”和“Group”列表中选择“您的工作区名称”。
- 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...
- 在Xcode中,右键单击要添加swift文件的文件夹并选择new file…
- Select swift > Next > Name your file > Create
- 选择swift >下一个>名称创建您的文件>
- 在您的框架中创建swift文件,在Xcode中右键单击要添加swift文件的文件夹,并选择新文件…选择swift >下一个>名称创建您的文件>
- Select your framework in Xcode -> Build your framework
- 在Xcode中选择您的框架——>构建您的框架
- 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
- 右键点击Xcode's files section > New file…>操场
- import
- 进口
- Call your method
- 打电话给你的方法
- 创建游乐场->游乐场也应该是你的工作空间的一部分,如果不把它拖到你的工作空间中,它会自动添加。右键点击Xcode's files section > New file…调用你的方法
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?
注意:如果你只是想添加类而不是框架到你的操场,检查链接如何将我自己的类导入到Xcode 7的操场上?
#4
3
I had my playground working fine but found that when I quit Xcode and re-opened it, I would get an error like this:
我的游乐场运行得很好,但当我退出Xcode重新打开它时,我发现会出现如下错误:
error: /var/folders/ft/bmk8wh6s5ms4my2pxhn3qbp40000gn/T/playground1-9216e0..swift:3:8: error: no such module 'Playground_Sources' import Playground_Sources
错误:/var/folders/ft/bmk8wh6s5ms4my2pxhn3qbp40000gn / T / playground1 - 9216 e0 . .错误:没有这样的模块“Playground_Sources”导入Playground_Sources
... where "Playground" is the name of my playground file.
…“游乐场”是我的游乐场文件的名字。
The solution to this (in Xcode 9.0) is to:
这个(Xcode 9.0)的解决方案是:
- click any playground file in your workspace
- 单击工作区中的任何游乐场文件
- show the Utilities panel (
opt
-cmd
-0
) - 显示实用程序面板(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.
在我的例子中,它被设置为macOS。我把它改成了iOS,然后又改成了macOS(不需要重新编译),它开始运行得很好……直到我关闭Xcode并再次打开它。
This is the only solution I found to work with macOS playgrounds. This bug still exists in Xcode 9.3.
这是我在macOS操场上找到的唯一解决方案。这个错误仍然存在于Xcode 9.3中。
#5
2
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.
苹果支持的解决方案是调整我的Xcode首选项。位置:高级配置为“遗留”。通过更改为“Unique”,并取消我试图插入的任何路径,Playgrounds现在可以导入框架。
#6
0
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.
我不认为Xcode有点像黑盒子。我没有声称这将为其他人工作,但对我来说,在同样的问题后,创建一个新操场和一个新的框架目标,和所有其他的提出解决方案,最后是删除工作区文件工作,(对于我来说)与cocoapods再生。
I have no idea why this worked.
我不知道为什么会这样。
#1
21
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.
对于上述解决方案中任何一个都不起作用的一些解决方案(Xcode构建路径设置已经设置为Unique),我找到了一个解决方案。
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 :
这个框架必须建立在一个iOS模拟器设备(列表中的任何一个)的方案中,而不是一个通用的iOS设备,因为操场不支持它。这个对我很有用:
#2
13
Here's how I proceed for OS X with Xcode 7.1:
以下是我如何用Xcode 7.1操作OS X:
-
Create new project: OS X Cocoa Framework, Swift. For this example I named it "TestPlaygroundFMK".
创建新项目:OS X Cocoa框架,Swift。对于这个示例,我将它命名为“TestPlaygroundFMK”。
-
Create a new Swift file.
创建一个新的Swift文件。
-
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.
在菜单栏中,单击File > Save As Workspace。
-
Create a new Playground and save it inside the project folder next to the Swift files.
创建一个新的运动场,并将它保存在Swift文件旁边的项目文件夹中。
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
5
-
Create a workspace as below:
创建如下所示的工作区:
- Choose File > New > Workspace
- 选择File >新的>工作区
- 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
现在,使用包含类的swift文件创建一个Cocoa框架
- Choose File > New > Project
- 选择文件>新的>项目
- Select Cocoa framework > Next
- 接下来选择Cocoa framework >
- Enter Product Name and Language > Next
- 接下来输入产品名称和语言>
- Before clicking create, in 'Add to:' and 'Group' list choose 'Your workspace name' as shown
- 在单击create之前,在“Add to:”和“Group”列表中选择“您的工作区名称”。
- 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...
- 在Xcode中,右键单击要添加swift文件的文件夹并选择new file…
- Select swift > Next > Name your file > Create
- 选择swift >下一个>名称创建您的文件>
- 在您的框架中创建swift文件,在Xcode中右键单击要添加swift文件的文件夹,并选择新文件…选择swift >下一个>名称创建您的文件>
- Select your framework in Xcode -> Build your framework
- 在Xcode中选择您的框架——>构建您的框架
- 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
- 右键点击Xcode's files section > New file…>操场
- import
- 进口
- Call your method
- 打电话给你的方法
- 创建游乐场->游乐场也应该是你的工作空间的一部分,如果不把它拖到你的工作空间中,它会自动添加。右键点击Xcode's files section > New file…调用你的方法
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?
注意:如果你只是想添加类而不是框架到你的操场,检查链接如何将我自己的类导入到Xcode 7的操场上?
#4
3
I had my playground working fine but found that when I quit Xcode and re-opened it, I would get an error like this:
我的游乐场运行得很好,但当我退出Xcode重新打开它时,我发现会出现如下错误:
error: /var/folders/ft/bmk8wh6s5ms4my2pxhn3qbp40000gn/T/playground1-9216e0..swift:3:8: error: no such module 'Playground_Sources' import Playground_Sources
错误:/var/folders/ft/bmk8wh6s5ms4my2pxhn3qbp40000gn / T / playground1 - 9216 e0 . .错误:没有这样的模块“Playground_Sources”导入Playground_Sources
... where "Playground" is the name of my playground file.
…“游乐场”是我的游乐场文件的名字。
The solution to this (in Xcode 9.0) is to:
这个(Xcode 9.0)的解决方案是:
- click any playground file in your workspace
- 单击工作区中的任何游乐场文件
- show the Utilities panel (
opt
-cmd
-0
) - 显示实用程序面板(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.
在我的例子中,它被设置为macOS。我把它改成了iOS,然后又改成了macOS(不需要重新编译),它开始运行得很好……直到我关闭Xcode并再次打开它。
This is the only solution I found to work with macOS playgrounds. This bug still exists in Xcode 9.3.
这是我在macOS操场上找到的唯一解决方案。这个错误仍然存在于Xcode 9.3中。
#5
2
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.
苹果支持的解决方案是调整我的Xcode首选项。位置:高级配置为“遗留”。通过更改为“Unique”,并取消我试图插入的任何路径,Playgrounds现在可以导入框架。
#6
0
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.
我不认为Xcode有点像黑盒子。我没有声称这将为其他人工作,但对我来说,在同样的问题后,创建一个新操场和一个新的框架目标,和所有其他的提出解决方案,最后是删除工作区文件工作,(对于我来说)与cocoapods再生。
I have no idea why this worked.
我不知道为什么会这样。