NavigationSplitView crashes if I select an item in the sidebar after I removed other items

My NavigationSplitView crashes if I select an item in the sidebar after I removed other items: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c44d488c)

How to reproduce:

The app crashes: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c44d488c)

It occurs if I use just Strings or Core Data objects (I tried with plain String because I thought it was maybe an issue with Core Data but it’s not apparently).

What is wrong? Is that a bug?

Filed #FB13561900 for this.

Replies

Here is the code to reproduce the issue:

import SwiftUI

@main
struct FB_SwiftUI_NavigationSplitView_Delete_SelectApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

enum SelectionString: Hashable {
    case all
    case item(String)
}

struct ContentView: View {
    @State private var selectionString: SelectionString?
    @State private var strings: [String] = []

    var body: some View {
        NavigationSplitView {
            List(selection: $selectionString) {
                Section {
                    NavigationLink(value: SelectionString.all) {
                        Text("All")
                    }
                }
            
                Section {
                    ForEach(strings.sorted(), id: \.self) { string in
                        NavigationLink(value: SelectionString.item(string)) {
                            Text(string)
                        }
                    }
                }
            }
            .toolbar {
                ToolbarItem {
                    Button("Add") {
                        strings.append(UUID().uuidString)
                    }
                }
            }
        } detail: {
            if let selectionString {
                switch selectionString {
                case .all:
                    Text("All")
                
                case .item(let string):
                    Button(role: .destructive) {
                        if let index = strings.firstIndex(of: string) {
                            strings.remove(at: index)
                            self.selectionString = nil
                        }
                    } label: {
                        Text("Delete")
                    }
                }
            } else {
                Text("Select an item")
            }
        }
    }
}

Is it because you're setting self.selectionString = nil, and nil isn't really something that can be shown in the detail part of the split view?

  • It can, it displays Text("Select an item").

Add a Comment

I realized the issue is actually caused by the use of Section. If I keep my same code I shared but remove the sections in the sidebar List, I don’t have the issue anymore. If I keep at least one section, it crashes.

List(selection: $selectionString) {
     NavigationLink(value: SelectionString.all) {
        Text("All")
     }
            
     ForEach(strings.sorted(), id: \.self) { string in
        NavigationLink(value: SelectionString.item(string)) {
            Text(string)
        }
    }
}