Answered on another forum - https://stackoverflow.com/questions/67073374/how-can-i-pass-data-from-a-playground-page-to-another-playground-page-in-swift-p/67126836#67126836.
You'll want to use PlaygroundKeyValueStore. This works similar to UserDefaults, but on playgrounds. Keep in mind that it only deals with PlaygroundValues, so you'll have to wrap your value.
You can do something like:
public var myVariable = 0 {
didSet {
PlaygroundKeyValueStore.current.keyValueStore["myKey"] = .integer(myVariable)
}
}
Then, on the other page, you can retrieve the value with:
guard let keyValue = PlaygroundKeyValueStore.current.keyValueStore["myKey"],
case .integer(let storedValue) = keyValue else {
// Deal with absence of value
}
// Use retrieved value
You should probably also store you keys on an enum, for example, to avoid mistyping.
enum StoredVariableKeys: String {
case myVariable1
/* ... */
}
And use this value for your key, like
let myKey = StoredVariableKeys.myVariable1.rawValue;
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: