'How to set a custom environment key in SwiftUI?
Creating a custom environment key works, but if I wish to set the value in a view, XCODE doesn't allow it. But the predefined environment values can be set. What am I doing wrong?
struct ResetDefault: EnvironmentKey {
static var defaultValue: Bool = false
}
extension EnvironmentValues {
var resetDefault: Bool {
get { self[ResetDefault.self] }
set { self[ResetDefault.self] = newValue }
}
}
struct ResetView: View {
@Environment(\.resetDefault) var reset
var body: some View {
Text("Reset").onAppear() {
reset = true. // Cannot assign to property: 'reset' is a get-only property
}
}
}
Solution 1:[1]
The Environment
is used to pass values in parent > child
direction, so value is set for usage. If you want to change internal of environment value then you need to wrap it somehow, possible variants are binding or reference type holder.
Here is an example of usage based on binding (similar to how .editMode and .presentationMode work)
struct TestResetEnv: View {
@State private var isActive = false
@State private var reset = false
var body: some View {
VStack {
Text("Current: \(reset ? "true" : "false")")
Button("Go") { self.isActive.toggle() }
if isActive {
ResetView()
}
}.environment(\.resetDefault, $reset) // set for children as env!!
}
}
struct ResetDefault: EnvironmentKey {
static var defaultValue: Binding<Bool> = .constant(false)
}
extension EnvironmentValues {
var resetDefault: Binding<Bool> {
get { self[ResetDefault.self] }
set { self[ResetDefault.self] = newValue }
}
}
struct ResetView: View {
@Environment(\.resetDefault) var reset
var body: some View {
Text("Reset").onAppear() {
self.reset.wrappedValue.toggle() // << change wrapped !!
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |