1

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.

1 Answer 1

3

Assign to property directly, like

case 4:
    self.animal = Animal.Dog
case 3:
    self.animal = Animal.Cat
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.