M4L15 Challenge crash and don't save rate with Picker

Hi there, I have a crash when i use live mode, for rate a title with Picker.

And It not save rate variable using the following code in my Staging view, and BookModel

Staging view

            Text("Rate \(book.title)")
            Picker ("Rate this", selection: $selectedRate) {
                Text("1").tag(1)
                Text("2").tag(2)
                Text("3").tag(3)
                Text("4").tag(4)
                Text("5").tag(5)
            }
            
            
            .pickerStyle(SegmentedPickerStyle())
            .onChange(of: selectedRate, perform: { value in
                model.updateRating(forId: book.id, rating: selectedRate)
            })

BookModel

    func updateRating(forId: Int, rating: Int) {
        
        if let index = books.firstIndex(where: { $0.id == forId }) {
            books[index].rating = rating
        }  
    }

Any helps? I debug, it, and I think in the view model reload the data. But for me, it the same code like the solution. I try to put together model and data equal to solution, but app crash too.

Thanks!

I did it private in github since i dont have a answer. For protect the code.

@Boby

Hi Boby

There is no need to protect your code in GitHub for this project since everyone who follows that course will have similar code.

Can you make it public again and I will have a look to see what the problem might be that is causing the crash.

Okay ! I change it to public.

The crash is caused by changes in iOS from when this course was first created (back in 2020). It means that instead of using a NavigationView in your ContentView, you now need to use NavigationStack.

The process of Navigating from one View to another in a NavigationStack is that each View you navigate to, via the NavigationLink, is placed on top of the current View just like a stack of cards. When you tap the < Back button it pops that card off the stack and throws it away to reveal the previous View.

Making that change will stop the PagesBookView from jumping back to the Staging View.