Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to include Playgrounds in a Swift Package?

When I publish a Swift Package, I like to include some Playgrounds to show examples of how to use the package. Prior to Swift 5.4, I was able to do this in a relatively straightforward way, but now none of my Playgrounds ever sees the contents of my package module. It doesn't make any sense to me.

I've followed the exact pattern illustrated by Apple's own WWDC videos, which is to have a Playgrounds/ folder as a peer to your Sources/ folder, and then just import the package module.

The import works, but the Playground always reports that it cannot find any of the types from inside the module. They are all marked public, and as I mentioned before, these used to work just fine.

enter image description here

This doesn't work for me in Swift 5.4 or 5.5, in Xcode 12.5 or 13 Beta.

I assume there must be some setting or configuration I am not understanding. Can somebody please explain the best practice here?

For reference, here is one of my simple Swift Packages that has Playgrounds that currently do not work: https://github.com/dunesailer/Wordsmith

EDIT: It gets even stranger. I have a different Swift Package structured what appears to me to be the exact same way, but its Playgrounds work fine. Whaaat? That one is here: https://github.com/dunesailer/Aesthete

like image 869
B.T. Avatar asked Jun 13 '21 01:06

B.T.


People also ask

How do I add a playground to my Xcode project?

The setup is really simple, just open your project, then go to Xcode > File > New > Playground, and be sure to select the option to add it to your existing project.

How do I create a playground in Xcode 12?

Create your Playground To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

How do I write a Swift package?

To create a new Swift package, open Xcode and select File > New > Swift Package. Choose a name and select a file location. Select “Create Git repository on my Mac” to put your package under version control. On completion, the Swift package opens in Xcode and looks similar to a standard Xcode project.


2 Answers

If you move/ delete the folders Chemical Names, Work Titles and Person Names inside Playgrounds/, all the playgrounds work fine. It looks like Xcode doesn't support folders in Playgrounds/ (/ it's a bug).

So to work around this issue, just put all your playgrounds in the same folder.

EDIT: It looks like this works inconsistently. I've submitted a bug report/ Feedback for Developer Tools in Feedback Assistant.


A longer term workaround is to use DocC (arrived in Xcode 13 beta), which will support more advanced tutorials/ articles in the repo.

like image 60
Ben Butterworth Avatar answered Sep 28 '22 16:09

Ben Butterworth


Use workspace.

(Xcode: File: New: Workspace)

You use it as "container" for both your package, playground (or app target for that matter). The important thing is to add each of these to the workspace by using "Add files to ..." button in the lower-left section of the Xcode, and from there on, open the .xcworkspace file:

  1. to edit your package
  2. to run playground file.

enter image description here

enter image description here

You can follow instructions from here.

https://stackoverflow.com/a/67580251/13292535

like image 21
Sardorbek Ruzmatov Avatar answered Sep 28 '22 18:09

Sardorbek Ruzmatov