Save Data from Swift Playgrounds App (Mac)

How do you save data from a Swift Playgrounds App on the Mac? I get an error 'playgroundSharedDataDirectory is not supported in Swift Playgrounds'

I think it's doable, some of the Swift Playgrounds tutorials remember the pages I've completed.

This is with Playgrounds 4.5.1 on a Mac mini M2 Pro running OSX 14.7 (Sonoma)

Ideally, I'd like to save multiple 'documents' and allow the user to select which one they want to work with. The documents don't need to be visible to other Apps.

Thanks in advance

Sorry for the delay.

On the best of my knowledge. Saving data from a Swift Playgrounds app on the Mac can be done, but there are some restrictions due to the sandboxed environment of Swift Playgrounds. The error message 'playgroundSharedDataDirectory is not supported in Swift Playgrounds' indicates that you're trying to use a directory that's not accessible within the playground's sandbox. Here's how you can save data in Swift Playgrounds on the Mac:

  • UserDefaults?

  • FileManager (Temporary Directory):

  • CloudKit (for Persistent Storage):

    • If you want to save data persistently and allow users to access it across multiple devices, you can use CloudKit. However, CloudKit requires a more complex setup and involves creating a CloudKit container in your Developer account.

Unfortunately, as of Swift Playgrounds 4.5.1 on macOS Sonoma, there's no built-in way to save multiple 'documents' that are visible within the playground's interface and accessible to the user for selection. The sandboxed environment limits the playground's access to the file system.

Maybe other developers have better ideas?

Save Data from Swift Playgrounds App (Mac)
 
 
Q