Use Property wrappers on the @ObservableState properties #2938
-
I have a I used to mark any property in my states with it, like the following: @Reducer
struct MyFeature {
struct State {
@Defaults(key: "isDebugModeEnabled", defaultValue: false)
var isDebugModeEnabled: Bool
}
} But now with the @Reducer
struct MyFeature {
@ObservableState
struct State {
@Defaults(key: "isDebugModeEnabled", defaultValue: false) <-- Property wrapper cannot be applied to a computed property
var isDebugModeEnabled: Bool
}
} After seeing this, won't the user expect when he writes the above that the What's the recommended approach here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @hadiidbouk, this is just an issue with Swift macros in general, in particular ones that turn a stored property into a stored+computed property duo. This affects Apple's own There really is no universal, good solution to this problem. All I can say is that we are currently running a shared state beta right now that solves the particular problem you are having, that of backing a field with user defaults. You will be able to use it like so: @Shared(.appStorage("isDebugModeEnabled")) var isDebugModeEnabled: Bool So, you can either give the beta a spin now, or wait until the tools officially ship sometime in the near future. |
Beta Was this translation helpful? Give feedback.
Hi @hadiidbouk, this is just an issue with Swift macros in general, in particular ones that turn a stored property into a stored+computed property duo. This affects Apple's own
@Observable
and@Model
macros too.There really is no universal, good solution to this problem. All I can say is that we are currently running a shared state beta right now that solves the particular problem you are having, that of backing a field with user defaults. You will be able to use it like so:
So, you can either give the beta a spin now, or wait until the tools officially ship sometime in the near future.