如何将第三方框架导入Xcode运动场?

时间:2023-01-23 22:00:40

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

如何将第3部分框架导入Xcode游乐场?

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

Swift游乐场显然有一个框架导入机制,因为我们可以导入Cocoa, SpriteKit,而在OSX游乐场,xc游乐场(奇怪的是,xc游乐场在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?

我真正想做的是将代码隐藏到我的游乐场中,并能够显示一个最小的示例。对于如何将框架加载到快速游乐场有什么想法吗?

See also:

参见:

(This question is different because the request is to use a framework in Playground rather than simply regular swift files)

(这个问题不同,因为要求在运动场使用框架,而不是简单地使用普通的swift文件)

6 个解决方案

#1


19  

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开始,当游乐场是作为目标构建框架的工作空间的一部分时,就支持这个功能。在苹果开发论坛网站上有更多的细节,但是希望@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/Frameworks directory and then just import <#Module#> the same way as you import system Frameworks.

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

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

为OSX游乐场:/应用/Xcode6-Beta.app/内容/开发者/平台/ macosx .平台/开发者/SDKs/MacOSX10.10.sdk/系统/库/框架

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

和iOS:/ / Xcode6-Beta.app /内容/应用开发人员/平台/ iPhoneSimulator.platform /开发/ sdk / iPhoneSimulator8.0.sdk /系统/图书馆/框架/

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

但是在你这么做之前…按@Rick的回答去申请雷达。

#2


44  

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

目前还没有支持将自己的框架或应用程序代码导入游乐场的方法,除非将其粘贴到游乐场编辑器中。我们知道这是非常理想的功能,但一如既往,我们鼓励人们在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.

现在可以将您自己的框架导入到一个游乐场中。这提供了一种在应用程序和平台之间共享代码的方法,它们都可以导入框架。为此,您的游乐场必须与生成框架的项目位于同一个工作区。您必须已经构建了框架。如果是iOS框架,则必须为64位运行目的地(例如iPhone 5s)构建。您必须有一个主动方案,该方案至少构建一个目标(目标的构建位置将用于游乐场的框架搜索路径)。您的“构建位置”首选项(在高级“位置”设置中)不应该设置为“遗留”。如果您的框架不是Swift框架,那么“定义模块”构建设置必须设置为“Yes”。一旦所有这些条件都得到满足,导入框架将在游乐场中工作。

#3


1  

I've written a tutorial covering 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.

简而言之,如果您在相同的项目或工作空间中有了您的游乐场和框架,并且您的框架是为64位(甚至是iOS)构建的,那么它们可以很好地结合在一起。

#4


1  

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——平台iOS——使用子模块

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 .xcworkspace file.

2:确保你的项目被保存为一个工作区。从Xcode菜单栏:File >保存为工作区。关闭Xcode并从新的.xcworkspace文件中重新打开。

3: Add your library's .xcodeproj file 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 >将文件添加到“your_workspace_name”中。然后找到它:${SRCROOT}/Carthage/Checkouts/your_library_name/your_library_name.xcodeproj。

4: Find the Embedded Binaries and Linked Frameworks and Libraries section in your project's general settings. Add your library's .framework file to both.

4:在项目的一般设置中找到嵌入的二进制文件和链接框架和库。将库的.framework文件添加到这两个文件中。

5: Clean(⇧+⌘+K) and build(⌘+B).

5:清洁(⇧+⌘+ K)和构建(⌘+ B)。

6: Import your library in the playground.

6:把你的图书馆建在操场上。

#5


1  

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.

我通过将构建框架复制到build Products目录来解决这个问题,在该目录中,工作区中的游乐场也会搜索框架。注意:您还需要运行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.)
  • 添加一个脚本阶段,将框架从Carthage/Build/iOS/复制到BUILT_PRODUCTS_DIR(我在下面的示例中使用了一个swift脚本,但也可以使用bash、ruby python等)。
  • Run aggregate target Now you can import the framework(s) in a Playground
  • 运行聚合目标现在您可以在游乐场中导入框架

Additionally you can leverage the aggregate target in your app:

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

  • Embed frameworks from BUILT_PRODUCTS_DIR, not Carthage/Build/iOS/
  • 嵌入框架从BUILT_PRODUCTS_DIR,而不是Carthage/Build/iOS/
  • Add aggregate target to app scheme to re-establish frameworks after a clean.
  • 在app scheme中添加聚合目标,重新建立框架。

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

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

#6


0  

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-Header header 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-Header after import UIKit and you will be able to use the classes you imported to your project.

就像在Swift代码中使用Objective-C类一样,您可以将第三方库/类导入到项目中,Xcode会询问您是否要创建<#YourProjectName>-Bridging-Header文件。选择yes并导入该文件中需要的所有头文件。之后,在您的应用程序中,您可以简单地在导入UIKit之后导入<#YourProjectName>-Bridging-Header,您将能够使用导入到项目中的类。

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

更新:这实际上是错误的。请看下面我的评论。抱歉为任何混乱……

#1


19  

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开始,当游乐场是作为目标构建框架的工作空间的一部分时,就支持这个功能。在苹果开发论坛网站上有更多的细节,但是希望@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/Frameworks directory and then just import <#Module#> the same way as you import system Frameworks.

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

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

为OSX游乐场:/应用/Xcode6-Beta.app/内容/开发者/平台/ macosx .平台/开发者/SDKs/MacOSX10.10.sdk/系统/库/框架

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

和iOS:/ / Xcode6-Beta.app /内容/应用开发人员/平台/ iPhoneSimulator.platform /开发/ sdk / iPhoneSimulator8.0.sdk /系统/图书馆/框架/

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

但是在你这么做之前…按@Rick的回答去申请雷达。

#2


44  

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

目前还没有支持将自己的框架或应用程序代码导入游乐场的方法,除非将其粘贴到游乐场编辑器中。我们知道这是非常理想的功能,但一如既往,我们鼓励人们在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.

现在可以将您自己的框架导入到一个游乐场中。这提供了一种在应用程序和平台之间共享代码的方法,它们都可以导入框架。为此,您的游乐场必须与生成框架的项目位于同一个工作区。您必须已经构建了框架。如果是iOS框架,则必须为64位运行目的地(例如iPhone 5s)构建。您必须有一个主动方案,该方案至少构建一个目标(目标的构建位置将用于游乐场的框架搜索路径)。您的“构建位置”首选项(在高级“位置”设置中)不应该设置为“遗留”。如果您的框架不是Swift框架,那么“定义模块”构建设置必须设置为“Yes”。一旦所有这些条件都得到满足,导入框架将在游乐场中工作。

#3


1  

I've written a tutorial covering 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.

简而言之,如果您在相同的项目或工作空间中有了您的游乐场和框架,并且您的框架是为64位(甚至是iOS)构建的,那么它们可以很好地结合在一起。

#4


1  

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——平台iOS——使用子模块

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 .xcworkspace file.

2:确保你的项目被保存为一个工作区。从Xcode菜单栏:File >保存为工作区。关闭Xcode并从新的.xcworkspace文件中重新打开。

3: Add your library's .xcodeproj file 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 >将文件添加到“your_workspace_name”中。然后找到它:${SRCROOT}/Carthage/Checkouts/your_library_name/your_library_name.xcodeproj。

4: Find the Embedded Binaries and Linked Frameworks and Libraries section in your project's general settings. Add your library's .framework file to both.

4:在项目的一般设置中找到嵌入的二进制文件和链接框架和库。将库的.framework文件添加到这两个文件中。

5: Clean(⇧+⌘+K) and build(⌘+B).

5:清洁(⇧+⌘+ K)和构建(⌘+ B)。

6: Import your library in the playground.

6:把你的图书馆建在操场上。

#5


1  

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.

我通过将构建框架复制到build Products目录来解决这个问题,在该目录中,工作区中的游乐场也会搜索框架。注意:您还需要运行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.)
  • 添加一个脚本阶段,将框架从Carthage/Build/iOS/复制到BUILT_PRODUCTS_DIR(我在下面的示例中使用了一个swift脚本,但也可以使用bash、ruby python等)。
  • Run aggregate target Now you can import the framework(s) in a Playground
  • 运行聚合目标现在您可以在游乐场中导入框架

Additionally you can leverage the aggregate target in your app:

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

  • Embed frameworks from BUILT_PRODUCTS_DIR, not Carthage/Build/iOS/
  • 嵌入框架从BUILT_PRODUCTS_DIR,而不是Carthage/Build/iOS/
  • Add aggregate target to app scheme to re-establish frameworks after a clean.
  • 在app scheme中添加聚合目标,重新建立框架。

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

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

#6


0  

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-Header header 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-Header after import UIKit and you will be able to use the classes you imported to your project.

就像在Swift代码中使用Objective-C类一样,您可以将第三方库/类导入到项目中,Xcode会询问您是否要创建<#YourProjectName>-Bridging-Header文件。选择yes并导入该文件中需要的所有头文件。之后,在您的应用程序中,您可以简单地在导入UIKit之后导入<#YourProjectName>-Bridging-Header,您将能够使用导入到项目中的类。

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

更新:这实际上是错误的。请看下面我的评论。抱歉为任何混乱……