How does _AppStorage
interoperate with SwiftUI AppStorage
?
#32
-
The following does not properly inject values: withDependencies {
$0.userDefaults = .ephemeral()
$0.userDefaults.set("hello", forKey: "myKey")
} operation: {
MyView()
} where @AppStorage("myKey") var myKey = "init" It works when I replace However, Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @Zeta611! Sorry if this wasn't clear: they interoperate only with the default live implementation. In your case, you're using the It is partly working here because you defined your I'll think about rephrasing this to make it clearer. I'll also think if I could make |
Beta Was this translation helpful? Give feedback.
-
Thank you for the clarification! |
Beta Was this translation helpful? Give feedback.
Hey @Zeta611! Sorry if this wasn't clear: they interoperate only with the default live implementation. In your case, you're using the
ephemeral
storage, but the SwiftUI variant doesn't know anything about that. They interoperate when you don't overrideuserDefault
storage (or use the sameUserDefaults
instance), because they're writing to the same place, in the same fashion, and they both observe changes to this storage. It means that you can create aSettings
model that writes to@Dependency.AppStorage
, and pop this preference in anyView
elsewhere in the hierarchy usingSwiftUI
's@AppStorage
for example.It is partly working here because you defined your
View
in awithDependency
block, …