Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Swift Package Manager in Playground

Is it possible to use Swift Package Manager inside Xcode 9 Playground?

like image 627
Blazej SLEBODA Avatar asked Mar 11 '17 15:03

Blazej SLEBODA


People also ask

How do I add package manager to Swift?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

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 .

Does Swift have a package manager?

The Swift Package Manager is a tool for managing the distribution of Swift code. It's integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. The Package Manager is included in Swift 3.0 and above.


3 Answers

It is not possible to use the Swift Package Manager in a playground at this time. The reason for that is that the Swift Package Manager is primarily used outside of apple platforms.

Apple mentions this on the Swift Package Manager's GitHub:

Note that at this time the Package Manager has no support for iOS, watchOS, or tvOS platforms

The Swift Package Manager is especially useful for writing Swift for platforms such as Linux and Mac. Uses may include creating servers based written in Swift or writing programs that can be run without a user interface (think home automations and IoT). It is a great way to organize packages without using Pods or other third-party setups. When you build the code for production (or debugging) it will download and incorporate packages into the project.

Note: This may change in the future, but is current as of Swift 3

like image 56
Alec O Avatar answered Oct 24 '22 03:10

Alec O


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

Steps to setup a workspace with a Swift Package and a Playground:

  • 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 5
wolfrevo Avatar answered Oct 24 '22 02:10

wolfrevo


In Xcode 12, playgrounds can work seamlessly with swift packages in a project.

Please follow the steps below:

  1. Create a project named Playground:

    File → New → Project... (⇧⌘N) → Product Name: Playground

    ⚠️ You can name it whatever you want, here is just an example.

  2. Add a swift package for the project, such as SnapKit:

    File → Swift Packages → Add Package dependency... → https://github.com/SnapKit/SnapKit.git

  3. Create a playground and add it to the project created in the first step:

    File → New → Playground... (⌥⇧⌘N) → Add to: Playground

  4. Import the swift package (SnapKit) in the Playground:

    import SnapKit
    

If nothing else, you can use SnapKit in the Playground.

like image 1
jqgsninimo Avatar answered Oct 24 '22 02:10

jqgsninimo