Created
October 16, 2020 16:40
-
-
Save Gernot/9d61dff3d7579b7cdaa5ed6760ab502f to your computer and use it in GitHub Desktop.
Model with Properties form AppStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
import Combine | |
import PlaygroundSupport | |
struct Model { | |
@AppStorage(wrappedValue: true, "Test") var isEnabled | |
} | |
struct MyView: View { | |
@State var model = Model() | |
@AppStorage(wrappedValue: true, "Test") var valueFromPreferences | |
var body: some View { | |
Form { | |
Toggle("State", isOn: model.$isEnabled) | |
Toggle("Preferences", isOn: $valueFromPreferences) | |
} | |
} | |
} | |
PlaygroundPage.current.setLiveView(MyView()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot! This works, but I see a few issues:
class
, and for reasons that are out of scope here, that's not a good option for me. I am looking for extending a struct with a property, without making additional changes to the model.ObservableObject
already has a defaultobjectWillChange
publisher that does not need to be overridden.I went for a different approach in the meantime:
AppStorage
.