Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 846
pacification Avatar asked Feb 22 '20 13:02

pacification


People also ask

How do I add a playground to my Xcode project?

To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

How do I create a swift playground Xcode 12?

To use this new feature, start by opening up the Xcode 12 beta and create a new playground using the File > New > Playground command. Then, once you've picked your platform of choice and your playground has been created, convert it into an Xcode workspace by selecting File > Save As Workspace .


3 Answers

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.

like image 109
wolfrevo Avatar answered Oct 13 '22 22:10

wolfrevo


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.

like image 31
sas Avatar answered Oct 14 '22 00:10

sas


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.

like image 5
yo2bh Avatar answered Oct 13 '22 22:10

yo2bh