The code I test is the following.
ContentView:
import SwiftUI
let dataSourceSection1 = [Folder(id: 1, name: "Folder1"), Folder(id: 2, name: "Folder2")]
let dataSourceSection2 = [Folder(id: 3, name: "Folde3"), Folder(id: 4, name: "Folder4")]
struct ContentView: View {
@State private var columnVisibility = NavigationSplitViewVisibility.all
@State private var folderIdSelected: Folder.ID?
var body: some View {
NavigationSplitView(columnVisibility: $columnVisibility) {
List(selection: $folderIdSelected) {
Section("Section1") {
ForEach(dataSourceSection1) { folder in
Text("\(folder.id)")
}
}
Section("Section2") {
ForEach(dataSourceSection2) { folder in
Text("\(folder.id)")
}
}
}
} detail: {
let values = dataSourceSection1 + dataSourceSection2
if let folderId = folderIdSelected {
let folderName = values.first(where: {$0.id == folderId})?.name ?? "no folder selected"
Text("\(folderName) | \(folderId)")
}
}
}
}
Folder:
struct Folder: Identifiable, Hashable {
var id: Int
var name: String
}
MyTestSplitViewApp:
import SwiftUI
@main
struct MyTestSplitViewApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
When run the code the following steps produces a crash:
- Select Item(Folder) 3.
- Back.
- Collapse Section 2.
- Select Item(Folder) 2
- Back.
- Collapse Section 1.
- Expand Section 2.
- Select Item(Folder) 4
Crash : Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1076f95f8)
I try with different dataSource (Dictionary), surround Sections with NavigationView but don't solve the problem.