xcode 游乐场导入:没有这样的模块“Foo”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33144877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playground Import: No Such Module 'Foo'
提问by GoZoner
I've diligently followed the Apple instructions to import a custom module into a playground, including the instructions here. And yet I get:
我一直认真地按照 Apple 的说明将自定义模块导入到操场中,包括此处的说明。然而我得到:
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/./lldb/1874/playground1.swift:7:8: 错误:没有这样的模块 '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]
如何恢复到正常工作的 Playground 导入?[编辑:注意,有两个答案提供了将游乐场与框架相关联的详细说明;我遵循了这些说明,但没有运气。 解决方案需要在 Xcode 中重新配置一些东西;我的安装显然坏了]
Detailed images of the error and attempts:
Here is another attempt, based on the answer of @EricD, showing the directory structure, no dice.
采纳答案by GoZoner
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 nowimport frameworks.
来自 Apple 支持的解决方案是调整我的 Xcode 首选项。在 Locations :: Advanced 我的配置是“Legacy”。通过更改为“唯一”并撤消我尝试插入的任何路径,Playgrounds现在可以导入框架。
回答by Cedric
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 模拟器设备(列表中的任何一个)的方案构建,而不是 a Generic iOS Device
,因为 Playgrounds 不支持它。这个对我有用:
回答by Eric Aya
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".
Create a new Swift file.
Add a class to the file. The code has to be public.I made this for our example:
创建新项目:OS X Cocoa Framework、Swift。对于此示例,我将其命名为“TestPlaygroundFMK”。
创建一个新的 Swift 文件。
向文件中添加一个类。代码必须是公开的。我为我们的例子做了这个:
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.
立即构建项目。
在菜单栏中,单击文件 > 另存为工作区。
创建一个新的 Playground 并将其保存在 Swift 文件旁边的项目文件夹中。
The Playground must nothave the same name as the project.
Playground不能与项目同名。
Build the project again.
Import your framework in the Playground and use it:
再次构建项目。
在 Playground 中导入您的框架并使用它:
import TestPlaygroundFMK
Talk.sayHello()
回答by Anni S
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
- 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
创建一个工作区如下:
- 选择文件 > 新建 > 工作区
- 输入工作区名称,并指定其在文件系统中的位置
- 点击保存
现在,使用包含您的类的 swift 文件创建一个 Cocoa 框架
- 在您的框架中创建 swift 文件
- 在Xcode中右键单击要添加swift文件的文件夹并选择新文件...
- 选择 swift > Next > Name your file > Create
- 在 Xcode 中选择您的框架 -> 构建您的框架
- 创建 Playground -> Playground 也应该是你工作区的一部分,如果不把它拖到你的工作区,它会被自动添加。
- 右键单击 Xcode 的文件部分 > 新建文件... > Playground
- 进口
- 调用你的方法
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?
注意:如果您只是想向您的 Playground 添加类而不是框架,请查看链接如何将我自己的类导入到 Xcode 7 playground?
回答by Sebastien Martin
I had my playground working fine but found that when I quit Xcode and re-opened it, I would get an error like this:
我的 Playground 运行良好,但发现当我退出 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-9216e0..swift:3:8: 错误:没有这样的模块“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
) - under "Playground Settings" change the "Platform" to anything else then set it back.
- 单击工作区中的任何 Playground 文件
- 显示实用程序面板 (
opt
-cmd
-0
) - 在“游乐场设置”下,将“平台”更改为其他任何内容,然后将其设置回原处。
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 playgrounds 的解决方案。这个错误在 Xcode 9.3 中仍然存在。
回答by Tomas Jablonskis
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.
- 设置您的工作区和项目/框架。
- 删除派生数据(如何删除派生数据?)
- 不要建立你的项目
- 创建与 Swift 文件或 .xcodeproj 所在文件夹下的项目/目标/框架名称不同的 Playground 文件(无论您选择哪一个)
- 在所需的平台下构建您的项目(如果 Playground 适用于 macOS,则使用 macOS 目标构建项目等)
- 构建完成后,尝试向您的 Playground 添加一些代码并运行它。
P.S. you might need to restart Xcode before 6th step.
附注。您可能需要在第 6 步之前重新启动 Xcode。
回答by Adobels
Insert a var at the first line and re run
在第一行插入一个 var 并重新运行
var str = ""
回答by Chris Conover
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 有点像一个黑匣子并不夸张。我不声称这对其他人有用,但对我来说,在创建新的操场和新的框架目标以及所有其他建议的解决方案后遇到同样的问题后,最终有效的是删除工作区文件和(在我的情况)用可可豆再生它。
I have no idea why this worked.
我不知道为什么这有效。
回答by zuizhe
Build the framework with release ,this worked for me. (Xcode 10.1, swift 4.2)
使用 release 构建框架,这对我有用。(Xcode 10.1,快速 4.2)