1

I have an environment object (we'll call it sample for this example) with multiple enums inside. I have a view that I would like to use over and over again instead of duplicating it a bunch of times. Inside that view is a button where I want to set the value of one of the enums.

I'm trying to figure out how to pass that button action as a parameter of the call to the view like this, TestView(enum: sample.enum1, destination: .main)

Here's some code:

class Sample: ObservableObject {

    @Published var enum1: Enum1 = .main
    @Published var enum2: Enum2 = .foo
    @Published var enum3: Enum3 = .add

    enum Enum1 {
        case main
        case test
    }

    enum Enum2 {
        case foo
        case blah
    }

    enum Enum3 {
        case add
        case remove
    }
}

struct MultipleUseView: View {
    @EnvironmentObject var sample: Sample
    @State var enum: Sample
    @State var destination: Sample

    var body: some View {
        Button {
            enum = destination
        } label: {
            "Tap Me"
        }
    }
}

struct TestView: View {
    @EnvironmentObject var sample: Sample

    var body: some View {
        MultipleUseView(enum: sample.enum1, destination: .main)
    }
}

The two @State variables I included in my sample code are just to show what's not working at the moment. All I need to figure out is how to pass those parameters to MultipleUseView, from TestView, so that the button in MultipleUseView ends up with an action of:

sample.enum1 = .main

3
  • This code snapshot is just a mass... Commented Aug 2, 2022 at 16:19
  • You may want to paste this code into Xcode and examine the errors. There are quite a few (not just related to your specific issue) Commented Aug 2, 2022 at 16:21
  • I was just typing too fast when writing up the example. Sorry for messing up the class. It's fixed now. Obviously I have this in the actual environment. I'm not asking for help with anything other than how to pass parameters of an enum (and the enum itself) to a view. Everything else already works just fine. My sample code is just to illustrate what I'm talking about. Commented Aug 2, 2022 at 16:24

1 Answer 1

1

With pretty amount of doubts I would suppose that you want to write a value into environment object by key path... if I'm right then it can be done as follows

struct MultipleUseView<T>: View {
    @EnvironmentObject var sample: Sample

    // bad practice to name vars as keywords, but if you insist...
    var `enum`: ReferenceWritableKeyPath<Sample, T>
    var destination: T

    var body: some View {
        Button {
            sample[keyPath: `enum`] = destination // << here !!
        } label: {
            Text("Tap Me")
        }
    }
}

struct TestView: View {
    @EnvironmentObject var sample: Sample

    var body: some View {
        MultipleUseView(enum: \.enum1, destination: .main)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.