ios 如何将自己的类从您自己的项目导入 Playground
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24045245/
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
How to import own classes from your own project into a Playground
提问by Markus Rautopuro
Assume a setup like this:
假设这样的设置:
- You have an Xcode 6 project, where you've implemented your own classes (say MyView and MyViewController) with both Objective-C and Swift
- You have added a Playground into your project
- 您有一个 Xcode 6 项目,您在其中使用 Objective-C 和 Swift 实现了自己的类(比如 MyView 和 MyViewController)
- 您已将 Playground 添加到您的项目中
In the Playground, it's possible to import modules (frameworks) like UIKit
with the import
keyword. How do you enable access to the project's other classes from the Playground?
在 Playground 中,可以像UIKit
使用import
关键字一样导入模块(框架)。如何从 Playground 访问项目的其他类?
Just trying to access project classes directly results with an error message: Use of unresolved identifier 'MyView'
只是尝试直接访问项目类会导致错误消息: 使用未解析的标识符“MyView”
采纳答案by Rick Ballard
As of Xcode 6.0 Beta 5, it is now possible to import your own frameworks into a playground. This provides a way to share code between your applications and playgrounds, which can both import your frameworks. To do this:
从 Xcode 6.0 Beta 5 开始,现在可以将您自己的框架导入到 Playground 中。这提供了一种在您的应用程序和 Playgrounds 之间共享代码的方法,它们都可以导入您的框架。去做这个:
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 (e.g. iPhone 5s), and must be built for the 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".
If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes".
You must add an import statement to your playground for the framework.
您的 Playground 必须与生成您的框架的项目位于同一工作区中。您的工作区必须包含一个生成框架的目标,而不是使用预先构建的框架。
您必须已经构建了您的框架。如果是 iOS 框架,则必须为 64 位运行目标(例如 iPhone 5s)构建,并且必须为 Simulator 构建。
您必须有一个至少构建一个目标的活动方案(该目标的构建位置将用于 Playground 的框架搜索路径)。
您的“构建位置”首选项(在 Xcode 的高级“位置”设置中)不应设置为“旧版”。
如果您的框架不是 Swift 框架,则“定义模块”构建设置必须设置为“是”。
您必须为框架添加一个 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; you can read about this "Auxiliary Sources" support at http://help.apple.com/xcode/mac/8.0/#/devfa5bea3af
在 Xcode 7 中,我们引入了另一种机制,您可以使用该机制将自己的类作为源导入,而不是导入框架;您可以在http://help.apple.com/xcode/mac/8.0/#/devfa5bea3af 上阅读有关此“辅助源”支持的信息
回答by Markus Rautopuro
I actually managed to refer to other Swift files in the current project by doing this:
通过这样做,我实际上设法引用了当前项目中的其他 Swift 文件:
- Create an empty Playground and place is somewhere under your project.
- Open the
YourPlayground.playground
bundle (yes, it's a bundle = directory) in Terminal. - Edit
contents.xcplayground
for example withvi
and add another section like this:
- 创建一个空的 Playground 并放置在您项目下的某个地方。
YourPlayground.playground
在终端中打开包(是的,它是一个包 = 目录)。contents.xcplayground
例如,编辑vi
并添加另一个部分,如下所示:
<?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>
<?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>
- Rename
section-1.swift
tosection-2.swift
(if you created the Playground from scratch, there should be an examplesection-1.swift
in your bundle) - Add a hard link(symbolic link doesn't seem to work) named
section-1.swift
which will point outside the bundle to your Swift class file like:
- 重命名
section-1.swift
为section-2.swift
(如果您从头开始创建 Playground,则section-1.swift
您的捆绑包中应该有一个示例) - 添加一个硬链接(符号链接似乎不起作用)命名
section-1.swift
,它将指向包外的 Swift 类文件,例如:
ln ../../Classes/MyView.swift section-1.swift
ln ../../Classes/MyView.swift section-1.swift
- Close Xcode and open the Playground again.
- Now there should be two sections, one with the contents of your Swift class file and the other one with the example content you got from creating the Playground from scratch.
- 关闭 Xcode 并再次打开 Playground。
- 现在应该有两个部分,一个是 Swift 类文件的内容,另一个是从头开始创建 Playground 的示例内容。
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 在他的回答中所指示的那样。
回答by nschum
Since Beta 5 of Xcode 6 it is possible to import your code if it is in a framework. What you need to do is create a framework target, add the Swift files there and in your playground do
从 Xcode 6 的 Beta 5 开始,如果您的代码在框架中,则可以导入您的代码。你需要做的是创建一个框架目标,在那里和你的操场上添加 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
. 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.)
记得制作你想看的代码public
。您需要先构建项目,然后才能在 Playground 中进行更改。(您还需要编辑 Playground 以触发重新执行。)
Important
重要的
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 文件与目标同名!如果这样做,导入似乎可以工作,但是当操场尝试执行时,您将收到以下错误:
Playground execution failed: error: Couldn't lookup symbols:
Playground 执行失败:错误:无法查找符号:
I wasted an hour on figuring that out. :)
我浪费了一个小时来弄清楚这一点。:)
回答by de Raad
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.
我无法使用此处的任何答案使其正常工作,因此我开始尝试并找到了一种简单的方法,可以将 swift 类导入到操场中。
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:
例如:
回答by qed
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.
这样,源文件中的更改将立即在您的 Playground 中生效。工作得很好。
Xcode 8.2, Swift 3.0.1, macOS Sierra
Xcode 8.2、Swift 3.0.1、macOS Sierra
回答by skywinder
All you have to do - is write in the beginning:
你所要做的 - 就是在开头写:
import ModuleName
(assuming your playground placed in the same workspace as framework/project)
(假设您的 Playground 与框架/项目位于同一工作区)
If it's doesn't work:
如果它不起作用:
Rebuild your project
Recreate playground and copy all from old playground there
重建你的项目
重新创建游乐场并从那里的旧游乐场复制所有内容
It solves a lot of strange errors with failed init's and importsof whatever!
它解决了许多奇怪的错误,包括失败的初始化和导入!