pacification
pacification

Reputation: 6018

How to import Swift Package in Playground?

Can i import Swift Package to my Playground that is in my workspace? I've already read the answer, but i think it's outdated, because now we can use Swift Packages in iOS projects.

Upvotes: 27

Views: 15860

Answers (5)

alex1704
alex1704

Reputation: 529

Xcode 14.2

  1. Create new project (ex. iOS app)
  2. File > New > Playground
  3. File > Add Packages (choose your package)
  4. Reload Xcode
  5. import Package in Playground

If Xcode still do not see package then:
File > Packages > Resolve Package Versions

Upvotes: 3

sas
sas

Reputation: 7542

Check out Arena, it allows you to create playgrounds with SPM dependencies embedded ready for use.

All it takes is

brew install finestructure/tap/arena
arena alamofire/alamofire

Update: The Swift Package Index has since gained Arena integration and you can create a playground for a package by clicking the "Try in a Playground" button on a package page.

Upvotes: 17

emehex
emehex

Reputation: 10538

I couldn't get @wolfrevo's answer working. However, I have a solution inspired by his:

  1. Open Xcode
  2. Create a New Project (File > New > Project)
  3. Choose Muliplatform > App > Next
  4. Project Name: Whatever > Next
  5. Create
  6. Now add the dependency (File > Swift Packages > Add Package Dependency)
  7. Paste package git url: https://github.com/maxhumber/Carlo.git > Next
  8. Select rules, or skip > Next
  9. Finish
  10. Create a new Playground file in this project by short-cutting Cmd+N
  11. Search Playground > Next
  12. Create

Upvotes: 2

wolfrevo
wolfrevo

Reputation: 2032

In Xcode 12, you can use Swift Packages with Xcode Playgrounds.

  • Open Xcode
  • Create a new Workspace (File > New > Workspace...)
  • Add the desired Swift package via File > Add Files to "Workspace Name"... selecting the package directory.
  • Create a new Playground via File > New > Playground.... Ensure that you select the Workspace we just created as the "Add to" and "Group" option during Playground creation (in the assistant where you select the Playground's location on disk).
  • You can now import the Package target into your Playground and starting exploring the Package's functionality.

You can find additional details and a sample project in this WWDC20 session: Explore Packages and Projects with Xcode Playgrounds

Ensure to check the new "Build active scheme" checkbox in the inspector for your existing Playgrounds.

Upvotes: 29

yo2bh
yo2bh

Reputation: 1436

Still now, Playground does not support the Swift Package Manager. When we try to add the Swift PM from Xcode using

File > Swift Packages > Add Package Dependency

the option is disabled.

enter image description here

An option is to use nef - it is an open-source project an lets you import third-party libraries in your Playgrounds. For now, it is compatible with Cocoapods but nef are working on open it for other dependency managers like Carthage and SPM.

About SPM is not an easy task, and it will be a bit different as Apple has prepared Xcode11 but I have good feelings and I think it could be ready for Sept-Oct.

Upvotes: 7

Related Questions