How to import Swift Package in Playground?
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.
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.
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.
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.