sivafe9540
sivafe9540

Reputation: 99

Pass Binding To ViewModifier And Modify It

SwiftUI:

This code is giving me so many headaches, it works without the Switch-Case statement

struct MyModifier: ViewModifier {
    @Binding var animal: Animal
    init(chosenAnimal: Binding<Animal>) {

        self._animal = chosenAnimal

        
    }
    
    func body(content: Content) -> some View {
        content
            .offset(x: scrollOffset + dragOffset, y: 0)
            ..... CODE THAT HELPS ME MAKING A SNAPPABLE HSTACK IS HERE... NOT IMPORTANT
                        
                        switch index {
                        case 4:
                            self._animal = Animal.Dog
                        case 3:
                            $animal = Animal.Cat
                        }
                    })
                     
            )
    }
}

enter image description here enter image description here Those are the images that show the errors

I have tried: adding mutating in func body(content: Content) -> some View but then it gives me an error saying I am not conforming ViewModifier.

Upvotes: 1

Views: 2429

Answers (1)

Asperi
Asperi

Reputation: 258443

Assign to property directly, like

case 4:
    self.animal = Animal.Dog
case 3:
    self.animal = Animal.Cat

Upvotes: 3

Related Questions