"Modifying state during view update" when porting swiftui landmark tutorial to mac

I'm learning swiftui and going through this tutorial from apple -- Apple Developer Documentation

I want to build mac apps moreso than just ios -- so I'm attempting to universalize this app so that it runs on ios and mac as I learn.

I'm noticing that the code used for ios when run on mac produces Modifying state during view update, this will cause undefined behavior. warnings when the 'Show Favorites' toggle is clicked.

struct LandmarkList: View {
    @State private var showFavoritesOnly = false
    var filteredLandmarks: [Landmark] {
        landmarks.filter { landmark in
            (showFavoritesOnly ? landmark.isFavorite : true)

    var body: some View {
        NavigationView {
            List {
                Toggle(isOn: $showFavoritesOnly) {
                    Text("Favorites only")
                ForEach(filteredLandmarks) { landmark in
                    NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                        LandmarkRow(landmark: landmark)

struct LandmarkList_Previews: PreviewProvider {
    static var appPreviewDevices: [String] {
        #if os(iOS)
        return ["iPhone SE (2nd generation)",
                "iPhone 12"]
        return ["Mac"]

    static var previews: some View {
        ForEach(appPreviewDevices, id: \.self) { deviceName in
                .previewDevice(PreviewDevice(rawValue: deviceName))

I'm trying to understand this error / how one might go about debugging it... My vague thought is that there must be @State inside the NavigationView on mac associated with rendering the highlight for the 'currently selected' LandmarkRow -- but that's just a very vague guess based on the fact that the ios version doesn't have any visual markers associated to a selected element within the NavigationView whereas the mac version renders the currently selected LandmarkRow within the NavigationView sidebar with a visual highlight effect ...

Is there an obvious way to diagnose where exactly the error is coming from? Or a good approach to change the code so that the mac version doesn't run afoul of this warning?

Any explanations or tips for how to adapt this code to mac in the most correct way so that warnings are not issued would be super helpful! :)


Terms of Service

Privacy Policy

Cookie Policy