Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a custom environment key in SwiftUI?

Tags:

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
        }
    }
}
like image 443
Sid Avatar asked Dec 01 '22 09:12

Sid


1 Answers

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 !!
        }
    }
}
like image 96
Asperi Avatar answered Dec 09 '22 19:12

Asperi