I don’t know how about you but I use Xcode Playgrounds to almost everything! It doesn’t matter if it’s a quick two-liner, checking out a new API, or exploring some subject. I have a ton of them and all with multiple pages.
This is one of my Xcode Playgrounds where I keep track on some topics:
When it’s done I move it from this Playground to another one that’s more focused. Also, this research is shared thru Dropbox with my friends so we can collaborate (yes I did heard about git 😉).
Dependencies in Xcode Playground
Yet there was something that I was missing all the time. And that thins was the ease of integrating external dependencies inside the Playground. There are ways that are a bit more complicated but there is a new sherif in town. That sherif is Xcode 12 beta.
Before I will continue I must say that I was kickstarted with this two great articles:
Here I have learned about the idea of a wrapper package.
This one contained a missing link on how to make it work with already existing playgrounds.
What was the issue
Unofficially solution from link #1 did not worked for me. Something just wont compile. On the other hand solution #2 is talking only about local swift packages. But it has the missing information what’s needs to be enabled for already existing playgrounds to make it work.
- Save your playground as a workspace: Xcode -> File -> Save As Workspace
2. Open this new workspace using Xcode 12; open any page inside and check
Build Active Scheme in Playground Settings.
3. Add new Swift Package that will be used to manage external dependencies.
Package.swift. Here as an example I’m using a Swift Package that streamlines working with Optionals in Swift OptionalAPI. Sometimes you need to set the minimum version of platforms:
// swift-tools-version:5.3 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "WrapPackage", platforms: [ .macOS(.v10_13), .iOS(.v11), .tvOS(.v11), .watchOS(.v3) ], products: [ .library( name: "WrapPackage", type: .dynamic, targets: ["WrapPackage"]), ], dependencies: [ // Dependencies go here .package( name: "OptionalAPI", url: "https://github.com/sloik/OptionalAPI", from: "2.2.1" ), ], targets: [ .target( name: "WrapPackage", dependencies: [ // And products of dependencies here .product(name: "OptionalAPI", package: "OptionalAPI"), ]), .testTarget( name: "WrapPackageTests", dependencies: ["WrapPackage"]), ] )
5. This package is downloaded as a dependency for the local package that was just created. So it’s only accessible only there. Luckily we can export it publicly with:
@_exported import OptionalAPI
Once again I have point out to the two articles I’m linking in this post. Without them I would not be able to connect the dots. I also encourage you to read them to get the better understanding of this solution.
Also if you are lazy as me and you want to just download the damn thing then here it is: https://github.com/sloik/SloikPresentations/tree/master/DependenciesInSwiftPlayground
Happy Playing 🤪