1
votes

I need to access an EnvironmentObject from a method within a View-Extension, so I cannot add an @EnvironmentObject property.

I tried to do it like

extension View {
    func myFunc() {
        let something = EnvironmentObject<MyType>()
    }
}

But that doesn't work - is there another/better way to get it from within the method?

1
This way it is definitely impossible - just opposite to EnvironmentObject concept. Would you give more context, so we could try to find different way? - Asperi

1 Answers

0
votes

You can pass the object as a parameter:

extension View {
    func myFunc(myType: MyType) -> some View {
        // do something with `myType`
    }
}