Search code examples
xcodedependenciesswift-playground

Adding Swift Package Dependencies in Xcode Blank Template Playgrounds


I'm using the Xcode Blank Template Playground because it’s very lightweight for testing some code. However, I encountered an issue when trying to add Swift package dependencies—the button is greyed out. For context, I am using Xcode Playground as a standalone "file," not within a project.

I also tried adding my custom package dependencies in a blank project, which was successful. Unfortunately, when I created a Playground within that project, I was still unable to import my library. Everything works fine when creating a Swift file, but it seems that inside a Playground, it does not work.

I also checked this video and is not what I needed: https://developer.apple.com/videos/play/wwdc2020/10096


So here is a picture working perfectly fine inside a Swift file (Blank Project) enter image description here

and here is with a playground (I usually do not use playgrounds in Projects but it was the only way to successfully add a library). enter image description here

Is there a way to add package dependencies in a lightweight environment like Xcode Playgrounds? I need individual small script/testing files, which is why I use Xcode Playgrounds.

Any suggestions would be greatly appreciated.


Solution

  • It took me some time to figure it out. If you need to share sources, you can use Xcode Playground. If you need to add modules to your Playground, like I did, you can use Swift Playgrounds (App) with a .playgroundbook file, which can be found right here. However, this was not ideal for me because my library required a terminal.

    enter image description here

    So in my case, a Command Line Tool is perfect. You can select from Swift, Objective-C, C++, or C. It also has a terminal and is lightweight, and you can add package dependencies.

    enter image description here