14

我有一个 swift 项目,其中包含通过 carthage 添加的一些框架。是否可以在项目内部的操场上使用这些框架以及如何使用它,因为

import Argo

不工作:(

4

5 回答 5

17

This stopped working at some point. Sigh

What I do now is

  • Create a macOS > Command Line Tool.
  • Create a Cartfile with github "ReactiveX/RxSwift" and run carthage update --platform iOS
  • Go to the command line tool target and add the frameworks from Carthage/Build/iOS in Linked frameworks and Libraries
  • Add the playground files.

At this point I’m able to run the playground files.

‍♂️</p>


A playground has access to external frameworks if it is part of a workspace that builds a target configured to access those frameworks.

If you want to add a playground to an existing carthage project, you only need to save the project as a workspace (File > Save as Workspace…), build the target, and you are done.

If you just want to distribute a playground with third party frameworks, you need to create a dummy workspace. Here is a step by step example for a playground with the RxSwift framework:

  1. Create a new Xcode project of type Cross-platform > Other > Empty. Name it RxPlayground.
    This will create this structure RxPlayground/RxPlayground.xcodeproj and open a blank Xcode.

  2. Download RxSwift with Carthage

    • Create a Cartfile with this line: github "ReactiveX/RxSwift" "swift4.0"
    • Run Carthage with carthage update --platform iOS.
  3. Add a playground to the project.

    • Click File > New > Playground…</li>
    • Choose the iOS > Blank template and name it Rx.playground
    • Right click the project node and choose “Add Files to RxPlayground”.
    • Select the Rx.playground and add it.
  4. Create a workspace

    • Click File > Save as Workspace…</li>
    • Save as Rx.xcworkspace
  5. Copy the frameworks to the products directory.

    • Close the project and open the Rx.xcworkspace
    • Create a Cross-platform > Other > Aggregate. Name it RxAggregate
    • Create a New Run Script Phase with the following content:
    cp -rv "${SRCROOT}/Carthage/Build/iOS/" "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}"

At this point, Xcode and the Finder look like this:

xcode

Note that Carthage/ and Cartfile.resolved appear when you run Carthage, Without them, your playground will be only a few Ks.

Finder

Lastly, build the project (⌘B). Now you can use the framework in your playground:

//: Playground - noun: a place where people can play
import RxSwift

_ = Observable<Void>.empty()
    .subscribe(onCompleted: {
        print("Completed")
    })

Sometimes the first time you build (⌘B) Xcode doesn’t notice the new framework (sigh). What I do is click on the target and back to a source file, or re-open the project. I don’t have an explanation why this happens.

于 2017-07-29T15:37:36.600 回答
5

为了使框架在 Playground 中工作,生成框架的项目必须包含在项目的工作区中。因此,要完成这项工作,您需要执行以下步骤:

  1. 如果您的项目不在工作区中,请通过File > Save As Workspace在 Xcode 中选择来为您的项目创建一个工作区。
  2. 将 .xcodeproj 文件从Carthage/Checkouts文件夹拖到您的工作区。
  3. 在您的框架目标上运行构建操作。
于 2015-05-17T19:22:09.163 回答
3

基于@Jano 的出色回答(感谢),我创建了一个功能齐全的游乐场来与 Carthage 框架进行交互:

https://github.com/backslash-f/carthage-playground

我以Charts框架为例:

图表示例

我用最新的 Xcode 进行了测试。

在我的机器上工作。

于 2018-06-22T23:56:42.547 回答
3

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

在此处查看更多信息: https ://github.com/richardnees/CarthagePlaygrounds

于 2017-04-18T15:28:35.673 回答
0

找到了如何做到这一点,而无需转换为工作区并将 Carthage/Checkouts 文件夹中的 .xcodeproj 文件添加到您的项目中。如果您有一些包含您想在 Playground 中使用的所有源的框架目标,则此方法有效。该框架必须在您的主要目标的“嵌入式框架”中。

  1. 从 Carthage/Build/IOS 将 Carthage .framework 添加到您的项目中
  2. 将其从项目树拖放到主要目标的“嵌入式框架”中
  3. 将 Carthage .framework 作为可选添加到您的框架目标(在目标成员中)
  4. 导入“您的框架目标”添加到游乐场

这对我有用

于 2018-11-08T14:20:40.283 回答