ios 如何将第 3 方框架导入 Xcode Playground?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24046160/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:27:44  来源:igfitidea点击:

How to I import 3rd party frameworks into Xcode Playground?

iosxcodemacosswiftxcode6

提问by Nate Murray

How do I import 3rd part frameworks into Xcode Playground?

如何将第三部分框架导入 Xcode Playground?

Swift Playground obviously has a framework import mechanism because we can import Cocoa, SpriteKit, and in an OSX Playground, XCPlayground(XCPlaygroundseems missing from iOS, oddly)

斯威夫特游乐场显然有一个框架,导入机制,因为我们可以导入CocoaSpriteKit以及在OSX游乐场,XCPlaygroundXCPlayground似乎来自iOS的缺失,奇怪)

What I'd really like to do is hide code from my playground and be able to show a minimal example. Any thoughts on how to load a Framework into Swift playground?

我真正想做的是在我的操场上隐藏代码并能够展示一个最小的例子。关于如何将框架加载到 Swift Playground 的任何想法?

See also:

也可以看看:

(This question is different because the request is to use a frameworkin Playground rather than simply regular swiftfiles)

(这个问题不同,因为请求是在 Playground 中使用框架而不是简单的常规swift文件)

采纳答案by ikuramedia

Edited: As of Beta5 this is now supported when the playground is part of the workspace that builds the framework as a target. There are more details on the Apple dev forums site, but hopefully @Rick Ballard will add them to his answer here and that should becoke the definitive answer for this question.

已编辑:从 Beta5 开始,当操场是将框架构建为目标的工作区的一部分时,现在支持此功能。Apple 开发论坛网站上有更多详细信息,但希望 @Rick Ballard 将它们添加到他的答案中,这应该成为这个问题的最终答案。

Don't do the below anymore!

不要再做下面的事情了!



For the short term, while there's no supported solution, if you're producing a Module/Framework you can copy it into the SDKs System/Library/Frameworksdirectory and then just import <#Module#>the same way as you import system Frameworks.

就短期而言,虽然没有支持的解决方案,但如果您正在生成模块/框架,您可以将其复制到 SDKsSystem/Library/Frameworks目录中,然后就像import <#Module#>导入系统框架一样。

For an OS X Playground: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks

对于 OS X 游乐场: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks

And for iOS: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/

对于 iOS: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/

But before you do that... go file a radar as @Rick says in his answer.

但是在你这样做之前......按照@Rick在他的回答中所说的那样去提交雷达。

回答by Rick Ballard

There is currently no supported way to import your own framework or app code into a playground, short of pasting it into the playground editor. We're aware that this is very desirable functionality, but as always we encourage people to "vote with bugreporter" at bugreport.apple.com

目前不支持将您自己的框架或应用程序代码导入到 Playground 中的方法,除非将其粘贴到 Playground 编辑器中。我们知道这是非常理想的功能,但我们一如既往地鼓励人们在 bugreport.apple.com 上“使用 bugreporter 投票”

Right now, if you're just trying to hide code for showing in an example, code folding in the editor might do the trick.

现在,如果您只是想隐藏代码以在示例中显示,编辑器中的代码折叠可能会奏效。

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, your playground must be in the same workspace as the project that produces your 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). 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) should not be set to "Legacy". If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes". Once all these conditions are fulfilled, importing your framework will work in a playground.

现在可以将您自己的框架导入到 Playground 中。这提供了一种在您的应用程序和 Playgrounds 之间共享代码的方法,它们都可以导入您的框架。为此,您的 Playground 必须与生成您的框架的项目位于同一工作区中。您必须已经构建了您的框架。如果是 iOS 框架,则必须为 64 位运行目标(例如 iPhone 5s)构建。您必须有一个至少构建一个目标的活动方案(该目标的构建位置将用于 Playground 的框架搜索路径)。您的“构建位置”首选项(在高级“位置”设置中)不应设置为“旧版”。如果您的框架不是 Swift 框架,则“定义模块”构建设置必须设置为“是”。

回答by iHunter

I've written a tutorialcovering import of custom frameworks in Beta 6

我写了一个教程,介绍了 Beta 6 中自定义框架的导入

In short, if you've got your playground and framework either in the same project or workspace, and your framework is built for 64 bits (even for iOS), they play very well together.

简而言之,如果您的 Playground 和框架位于同一个项目或工作区中,并且您的框架是为 64 位构建的(甚至适用于 iOS),那么它们可以很好地协同工作。

回答by Shiyason

source: http://qiita.com/ryokosuge/items/2551cd4faa9dca324342

来源:http: //qiita.com/ryokosuge/items/2551cd4faa9dca324342

If there is anyone who still had difficulty understanding how to go about it after reading the accepted answer, this may help.

如果有人在阅读已接受的答案后仍然难以理解如何去做,这可能会有所帮助。

It's not in English though so I will explain here:

虽然它不是英文的,所以我会在这里解释:

Using Carthage:

使用迦太基:

1: Setup your Cartfile in the root of the project, add your libraries and run:

1:在项目的根目录中设置您的 Cartfile,添加您的库并运行:

carthage update --platform iOS --use-submodules

carthage update --platform iOS --use-submodules

2: Make sure your project is saved as a Workspace. From the Xcode menu bar: File> Save As Workspace. Shutdown Xcode and reopen from the new .xcworkspacefile.

2:确保您的项目保存为Workspace. 从 Xcode 菜单栏中:File> Save As Workspace. 关闭 Xcode 并从新.xcworkspace文件重新打开。

3: Add your library's .xcodeprojfile in your workspace: File> Add files to "your_workspace_name". Then find it in: ${SRCROOT}/Carthage/Checkouts/your_library_name/your_library_name.xcodeproj

3:.xcodeproj在您的工作区中添加您的库文件:File> Add files to "your_workspace_name". 然后在:${SRCROOT}/Carthage/Checkouts/your_library_name/your_library_name.xcodeproj

4: Find the Embedded Binariesand Linked Frameworks and Librariessection in your project's general settings. Add your library's .frameworkfile to both.

4:在您的项目的常规设置中找到Embedded BinariesLinked Frameworks and Libraries部分。将您图书馆的.framework文件添加到两者中。

5: Clean(?+?+K) and build(?+B).

5:清理(?+?+K) 和构建(?+B)。

6: Import your library in the playground.

6:在操场上导入你的图书馆。

回答by Richard Kruse Nees

I solved it by copying the built frameworks to the Built Products Directory, where Playgrounds in the workspace also searches for frameworks. Note: you also need to run lipo and strip away unused architectures from the FAT binaries.

我通过将构建的框架复制到构建产品目录来解决它,工作区中的 Playgrounds 也在其中搜索框架。注意:您还需要运行 lipo 并从 FAT 二进制文件中去除未使用的架构。

The steps needed (in a nutshell):

所需步骤(简而言之):

  • Create an aggregate target
  • Add a script phase to copy the frameworks from Carthage/Build/iOS/ to BUILT_PRODUCTS_DIR (I use a swift script in the example below, but you can use also use bash, ruby python etc.)
  • Run aggregate target Now you can import the framework(s) in a Playground
  • 创建聚合目标
  • 添加脚本阶段以将框架从 Carthage/Build/iOS/ 复制到 BUILT_PRODUCTS_DIR(我在下面的示例中使用了 swift 脚本,但您也可以使用 bash、ruby python 等)
  • 运行聚合目标现在您可以在 Playground 中导入框架

Additionally you can leverage the aggregate target in your app:

此外,您可以利用应用程序中的聚合目标:

  • Embed frameworks from BUILT_PRODUCTS_DIR, not Carthage/Build/iOS/
  • Add aggregate target to app scheme to re-establish frameworks after a clean.
  • 从 BUILT_PRODUCTS_DIR 嵌入框架,而不是 Carthage/Build/iOS/
  • 将聚合目标添加到应用程序方案以在清理后重新建立框架。

Here is an example project setup as above: https://github.com/richardnees/CarthagePlaygrounds

这是上面的示例项目设置:https: //github.com/richardnees/CarthagePlaygrounds

回答by Peng90

Like using Objective-C classes in Swift code, you can import 3rd-party library/classes into your project and Xcode will ask you if you wanna create a <#YourProjectName>-Bridging-Headerheader file. Choose yes and import all header files you need in that file. After that, in your playground, you can simply do import <#YourProjectName>-Bridging-Headerafter import UIKitand you will be able to use the classes you imported to your project.

就像在 Swift 代码中使用 Objective-C 类一样,您可以将 3rd-party library/classes 导入到您的项目中,Xcode 会询问您是否要创建<#YourProjectName>-Bridging-Header头文件。选择 yes 并在该文件中导入您需要的所有头文件。之后,在您的 Playground 中,您可以简单地执行import <#YourProjectName>-Bridging-Headerafter import UIKit,您将能够使用您导入到项目中的类。

Update: this is actually incorrect. Please see my comment below. Sorry for any confusion...

更新:这实际上是不正确的。请看我下面的评论。抱歉有任何混淆...