unable to mutate struct var

I am not having any luck mutating a var in a structure, and yes, the method is defined as 'mutating'

here's the struct definition:

struct OnboardingSwipeableView: View{
    static var shared = OnboardingSwipeableView()

    @State var currentPage = 0
    var lock = NSLock()
    <snip>

here's the method:

 mutating func goToNextPage(){
        currentPage = self.currentPage+1
        
        print("OBM->goToNestPage: currentPage=\(self.currentPage)")
    }

however currentPage is not being updated. What gives?

I’m not sure what’s going on here, but I recommend that you start by simplifying your test case. I created a new app from the Multiplatform > App templates and then changed the content view to this:

struct ContentView: View {

    @State var currentPage = 0

    var body: some View {
        VStack {
            Text("\(currentPage)")
            Button("Next") {
                goToNextPage()
            }
        }
        .padding()
    }

    func goToNextPage() {
        self.currentPage += 1
    }
}

On running the app (Xcode 16.0 running on macOS 14.6.1) the number incremented every time I clicked the Next button.

Try repeating this test and see what you get.

Also, the fact that you mention NSLock is a concern, in that it suggests you have involve threads in same way. If so, are you sure that goToNextPage() is running on the main thread?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

unable to mutate struct var
 
 
Q