In Playgrounds how can you share data between files? In Xcode I used to use this method but it's not working in Playgrounds http://candycode.io/sharing-data-between-view-controllers-and-other-objects-in-swift/
How to share data amount sources in Playgrounds?
Rather than pointing us to a tutorial, could you walk us through a minimal example of what you’ve tried and what’s failing?
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
Sorry about that. It turns out that when I tried to create a new file in swift playgrounds (iPad) it created a new page so that's why I couldn't access classes and functions like a normally do. After using trying on the Mac it works fine. I'm not sure how to share data between PlaygroundBook chapters, is this possible?
I’m not 100% sure what context you’re working in but I’ll try to address the two most likely options:
All playgrounds support a shared data directory, returned by
, to which you can read and write files.playgroundSharedDataDirectory
Some playgrounds support the [
][refPBFR], which lets you get and set shared values. https://developer.apple.com/library/content/documentation/Xcode/Conceptual/swift_playgrounds_doc_format/PlaygroundKeyValueStoreClass.html#//apple_ref/doc/uid/TP40017343-CH41-SW1PlaygroundKeyValueStoreClass
With regards the last point, it seems to be support by Swift Playgrounds on iOS, with the values only persisting if you’re in a playground book.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
Hello Quinn,
been watching WWDC and your sessions since 2010, thanks for the
clarity of your performances...
I am having a similar concern here but rather related to iOS playgrounds.
I am using Playgrounds simply to prototype and test certain functionality
of my Apps and I needed to store a file that was created by my code
(myRecorded.wav) to some URL on the iPad where it is accessible for other
Apps. So, somewhere in the Playground Container File or even in the
Files App.
Is there a publicly shared directory on iOS Playgrounds and what would
the URL be which I had to store my 'myRecorded.wav' file into? Or the URL
in the Files App for that matter?
Thanks in advance for your help!
Is there a publicly shared directory on iOS Playgrounds and what would the URL be which I had to store my
file into? Or the URL in the Files App for that matter?myRecorded.wav
No to both. Even with a fully-fledged iOS app, arbitrary file sharing is restricted by the sandbox. With Swift Playgrounds you have a sandbox within a sandbox (the main app is sandboxed, and the playground is sandboxed within that) which makes things trickier.
For a normal app there are standard ways to shared files — things like
UIDocumentInteractionController
and
UIActivityViewController
— but I’m not sure whether they’ll work from a playground. I played around with
UIDocumentInteractionController
for a bit but couldn’t get it it work in a playground )-:
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks for clarifying.
But that still leaves us with one (unanswered) question.
In the Playgrounds app on the iPad, I am allowed to create new
riders in the tab bar. This opens up a file that is shared between all
modules and pages of the very same Playground - and I think
even cross-playground.
So, why can I create this new file and - if it is sandboxed - why does
it appear in my playground's project bundle as a dedicated file
in the Files App and why can I copy/open it from here into every
other Application? To me it seems to be in the public domain.
So if I can share code between several Playgrounds/Modules/Pages
shouldn't I also be able to share a file that my code creates to the very
same folder in the project bundle to make it available on the next page
or for another Module/Playground. Isn't this the whole point of making
sharing public in the Playgrounds App?
So the big question is - what is this URL in the project file to write my
composed file to, like I do when creating a new code-file in the project?