get EnvironmentObject property within method

237 Views Asked by At

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

There are 1 best solutions below

0
On

You can pass the object as a parameter:

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