Can @AppStorage be used in the Environment in SwiftUI, if so, how would you do it?
I know you can send the value for the @AppStorage from one view to another using @Bindings as a general wondering I would like to know if its possible to put it in the environment. I don't have a practical example as to when this would be applicable, but I was wondering if it was possible.
Would this be crazy enough to work? I think you will only store the value and it won't be stored in the UserDefault.
struct RootView: View {
@AppStorage("userPreferredDisplayMode") private var userPreferredDisplayMode: String = "automatic"
@Environment(\.userPreferredDisplayMode) private var envUserPreferredDisplayMode: String
var body: some View {
Text(title)
.environment(\.userPreferredDisplayMode, envUserPreferredDisplayMode)
}
}
Turns out that you can.