Using Swift Packages in a Playground in Xcode 11?

Question: how can I import local Swift Packages into a playground?

Our project has lots of local frameworks and a few local Swift packages, all in a big workspace. I recently added a playground to our workspace, and made a scheme that builds all the framework and package targets.

I'm able to import the local frameworks just fine, but for some reason, none of our local Swift packages seem to be recognized within the playground.

Any help would be appreciated. Thanks.


In order to make a locally-defined Swift Package usable within an Xcode playground, it is necessary to create a wrapper framework that links to the packages you want to use in the playground. This framework does not need any code in it.

In the playground, you must import then import both the wrapper framework and the Swift packages that you want to use in the playground code.

But beware...

The root of my problem turned out to be a bizarre conflict between ReactiveSwift and Apple's SwiftProtobuf (upon which the package I'm testing depends).

The conflict only happens if I add ReactiveSwift (6.2.x) into my wrapper framework's link phase, from Carthage/Build/iOS. (Adding in other frameworks from the same folder causes no problems—only Reactive Swift causes issues).

When ReactiveSwift is in the link phase of the wrapper framework, then Playground loses the ability to lookup symbols, and reports errors like this:

error: Couldn't lookup symbols:
  SwiftProtobuf.BinaryDecodingOptions.init() -> SwiftProtobuf.BinaryDecodingOptions
  SwiftProtobuf.BinaryDecodingOptions.init() -> SwiftProtobuf.BinaryDecodingOptions

If I don't link ReactiveSwift in the wrapper, then everything works fine.

The most bizarre aspect of this, is that I'm not importing ReactiveSwift in the Playground... so why should it matter whether it's linked to the wrapper framework?

It honestly seems like a compiler bug... but if anyone can demystify what's happening here, that would really be appreciated.

1 Like

Some of this might work incidentally, but the gist is that using Swift packages in a playground isn't supported today.

Some of this might work incidentally, but the gist is that using Swift packages in a playground isn't supported today.

Seems that way. Hopefully Apple fixes this in the next Xcode release, because one would expect that defining a dynamic or static target in the same workspace as the playground is all that's required for importing it into a playground. It seems pretty inconsistent for packages to be some kind of special case.