Work with shared files in Swift Playgrounds on Mac
Many playground books have modules that contain shared Swift files. These files contain code you can use on any page of your playground. In many playground books, you can also add your own modules and Swift files.
View modules and shared files
If a playground book has modules and shared Swift files, you can view them in the left sidebar.
In the Swift Playgrounds app on your Mac, open the playground , then click near the top left to open the sidebar.
The modules appear below all the pages in the list. Scroll down, if necessary, to see the modules.
The files in each module appear below the module name.
Add a module
In many playgrounds books, you can add modules.
Note: In playground books that are set to limited mode, you can’t add or delete any modules. (In a limited mode playground, the Add button doesn’t appear next to Source Code when you move your cursor there.)
In the open playground book in the Swift Playgrounds app on your Mac, click near the top left to open the sidebar.
In the sidebar, move your cursor to Source Code, then click next to it. (If doesn’t appear, it means you can’t add a module to the playground.)
A new, untitled module appears below the other modules.
Enter a name for the new module, then press Return.
Add a file
You can add Swift files to some playground books, including limited mode playground books.
In the open playground book in the Swift Playgrounds app on your Mac, click near the top left to open the sidebar.
In the sidebar, move your cursor next to the module you want to add the file to, then click next to the module name.
A new, untitled file appears below the other files in the module.
Enter a name for the new file, then press Return.
The new file opens in a separate tab. You can click in the coding area to enter code.
You can have multiple files open at the same time—each file opens in a separate tab. You can switch between open files by clicking the tabs.
Rename a module or shared file
Note: If you rename a module, make sure you update any files that refer to it. Commands that include the old name won’t work as intended.
In the open playground book in the Swift Playgrounds app on your Mac, click near the top left to open the sidebar.
In the sidebar, Control-click the name you want to change, then choose Rename.
Enter a new name, then press Return.
Delete a module or shared file
WARNING: If you delete a module or shared Swift file, you can’t get it back. The code it contains is removed and commands in other files that refer to the deleted one no longer work.
In a playground in the Swift Playgrounds app on your Mac, click near the top left to open the sidebar.
Control-click the file or module you want to delete, then choose Delete.