Alexey Poimtsev
Alexey Poimtsev

Reputation: 2847

How to use frameworks imported with carthage in swift playground

i have a swift project with some frameworks added via carthage. Is it possible to use those frameworks in playground inside project and how to use it, because

import Argo

doesn't work :(

Upvotes: 14

Views: 5223

Answers (5)

backslash-f
backslash-f

Reputation: 8193

Based @Jano's great answer (thanks for that), I created a fully functional playground to interact with Carthage frameworks:

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

I included the Charts framework as an example:

Charts example

I tested with latest Xcode.

"Works on my machine." 👍🏻

Upvotes: 3

Jano
Jano

Reputation: 63707

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.

🤷🏻‍♂️


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…
    • 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…
    • 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.

Upvotes: 17

sergiy batsevych
sergiy batsevych

Reputation: 385

Found how to do this without converting to workspace and adding .xcodeproj file from the Carthage/Checkouts folder into your project. This works if you have some framework target that contains all sources you want to use in playground. This framework must be in "Embedded frameworks" of your main target.

  1. Add Carthage .framework to your project from Carthage/Build/IOS
  2. Drag and drop it from your project tree into "Embedded frameworks" for your main target
  3. Add Carthage .framework to your framework target as Optional(In target Membership)
  4. Add import "Your framework target" into playground

That worked for me

Upvotes: 0

Richard Kruse Nees
Richard Kruse Nees

Reputation: 61

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.

See more here: https://github.com/richardnees/CarthagePlaygrounds

Upvotes: 3

Niels
Niels

Reputation: 583

In order for the framework to work within a playground, the project that produces the framework must be included in the workspace for your project. So to make this work you need to follow these steps:

  1. If your project is not inside a workspace, create a workspace for your project by choosing File > Save As Workspace in Xcode.
  2. Drag the .xcodeproj file from the Carthage/Checkouts folder into your workspace.
  3. Run the build action on your framework target.

Upvotes: 5

Related Questions