[英]How to import own classes from your own project into a Playground
Assume a setup like this: 假设这样的设置:
In the Playground, it's possible to import modules (frameworks) like UIKit
with the import
keyword. 在Playground中,可以使用
import
关键字import
UIKit
等模块(框架)。 How do you enable access to the project's other classes from the Playground? 如何从Playground启用对项目其他类的访问?
Just trying to access project classes directly results with an error message: Use of unresolved identifier 'MyView' 只是尝试直接访问项目类会产生错误消息: 使用未解析的标识符“MyView”
As of Xcode 6.0 Beta 5, it is now possible to import your own frameworks into a playground. 从Xcode 6.0 Beta 5开始,现在可以将自己的框架导入游乐场。 This provides a way to share code between your applications and playgrounds, which can both import your frameworks.
这提供了一种在应用程序和游乐场之间共享代码的方法,这可以导入您的框架。 To do this:
去做这个:
Your playground must be in the same workspace as the project that produces your framework. 您的游乐场必须与生成框架的项目位于同一工作区中。 Your workspace must contain a target that produces the framework, instead of using a pre-built framework.
您的工作区必须包含生成框架的目标,而不是使用预先构建的框架。
You must have already built your framework. 您必须已经构建了框架。 If it is an iOS framework, it must be built for a 64-bit run destination (eg iPhone 5s), and must be built for the Simulator.
如果它是iOS框架,则必须为64位运行目标(例如iPhone 5s)构建,并且必须为Simulator构建。
You must have an active scheme which builds at least one target (that target's build location will be used in the framework search path for the playground). 您必须拥有一个活动方案,该方案至少构建一个目标(该目标的构建位置将用于操场的框架搜索路径)。
Your "Build Location" preference (in advanced "Locations" settings of Xcode) should not be set to "Legacy". 您的“构建位置”首选项(在Xcode的高级“位置”设置中)不应设置为“旧版”。
If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes". 如果您的框架不是Swift框架,则“定义模块”构建设置必须设置为“是”。
You must add an import statement to your playground for the framework. 您必须在操场上为框架添加import语句。
Once all these conditions are fulfilled, importing your framework will work in a playground. 一旦满足所有这些条件,导入框架将在游乐场中运行。
In Xcode 7 we introduced another mechanism that you can use to import your own classes as source, instead of importing a framework; 在Xcode 7中,我们引入了另一种机制,您可以使用它将您自己的类作为源导入,而不是导入框架; you can read about this "Auxiliary Sources" support at http://help.apple.com/xcode/mac/8.0/#/devfa5bea3af
您可以在http://help.apple.com/xcode/mac/8.0/#/devfa5bea3af上阅读有关此“辅助来源”支持的信息。
I actually managed to refer to other Swift files in the current project by doing this: 我实际上通过这样做设法引用当前项目中的其他Swift文件:
YourPlayground.playground
bundle (yes, it's a bundle = directory) in Terminal. YourPlayground.playground
包(是的,它是一个bundle =目录)。 contents.xcplayground
for example with vi
and add another section like this: vi
编辑contents.xcplayground
并添加另一个部分,如下所示: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <playground version='3.0' sdk='iphonesimulator'> <sections> <code source-file-name='section-1.swift'/> <code source-file-name='section-2.swift'/> </sections> <timeline fileName='timeline.xctimeline'/> </playground>
section-1.swift
to section-2.swift
(if you created the Playground from scratch, there should be an example section-1.swift
in your bundle) section-1.swift
重命名为section-2.swift
(如果你从头开始创建Playground,你的包中应该有一个示例section-1.swift
) section-1.swift
which will point outside the bundle to your Swift class file like: section-1.swift
的硬链接 (符号链接似乎不起作用),它将指向包外的Swift类文件,如: ln ../../Classes/MyView.swift section-1.swift
This way I can actually run code lying outside the Playground, but Xcode seems to crash more often when doing it like this. 这样我实际上可以在Playground外面运行代码,但是当这样做时,Xcode似乎更频繁地崩溃。
Edit: 编辑:
As of Xcode 6 beta 5 you're now able to refer to project files, as Rick Ballard instructs in his answer . 从Xcode 6 beta 5开始,你现在可以参考项目文件了,正如Rick Ballard在他的回答中指出的那样。
Since Beta 5 of Xcode 6 it is possible to import your code if it is in a framework. 从Xcode 6的Beta 5开始,如果代码在框架中,则可以导入代码。 What you need to do is create a framework target, add the Swift files there and in your playground do
你需要做的是创建一个框架目标,在那里和操场上添加Swift文件
import ModuleName
You can look up the module name in the build settings. 您可以在构建设置中查找模块名称。 It's usually the same as the target name.
它通常与目标名称相同。
Remember to make the code you want to see public
. 记得制作想要
public
的代码。 You'll need to build the project before changes are available in the playground. 您需要在操场中进行更改之前构建项目。 (You'll also need to edit the playground to trigger re-execution.)
(您还需要编辑操场以触发重新执行。)
Do not give the playground file the same name as the target! 不要给操场文件提供与目标相同的名称! If you do, importing seems to work but you'll get the following error when the playground tries to execute:
如果你这样做,导入似乎工作,但当操场尝试执行时你会得到以下错误:
Playground execution failed: error: Couldn't lookup symbols:
游乐场执行失败:错误:无法查找符号:
I wasted an hour on figuring that out. 我浪费了一个小时来搞清楚这一点。 :)
:)
I wasn't able to get it working using any of the answers here, so I started playing around and found a simple way that worked for me to import a swift class into a playground. 我无法使用这里的任何答案让它工作,所以我开始玩,并找到了一种简单的方法,可以让我将一个快速的课程导入游乐场。
Just create a playground in your project, theres a directory inside it called 'sources', just drag a copy of the swift class into that folder and the playground then will have access to it. 只需在项目中创建一个游乐场,其中的一个名为“sources”的目录,只需将swift类的副本拖到该文件夹中,然后操场就可以访问它。
For example: 例如:
I just put links to all my swift files in the Sources
folder: 我只是链接到
Sources
文件夹中的所有swift文件:
cd /path/to/project/MyPlayground.playground/Sources
ln -s ../../*.swift .
This way changes in your source file will take effect in your playground immediately. 这样,源文件中的更改将立即在您的游乐场生效。 Worked very nicely .
工作得非常好 。
Xcode 8.2, Swift 3.0.1, macOS Sierra Xcode 8.2,Swift 3.0.1,macOS Sierra
All you have to do - is write in the beginning: 所有你需要做的 - 写在开头:
import ModuleName
(assuming your playground placed in the same workspace as framework/project) (假设您的游乐场与框架/项目位于同一工作区)
Rebuild your project 重建您的项目
Recreate playground and copy all from old playground there 重新创建游乐场并从那里的旧操场复制
It solves a lot of strange errors with failed init's and imports of whatever! 它解决了许多奇怪的错误,失败的init和导入的任何东西!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.