Since iOS/iPadOS beta 6, I get a crash just by simply selecting an item in the sidebar of a navigation view. The selected item is part of an app mode, with conforms to the Observation protocol:
import SwiftUI
import Observation
@main
struct MREAApp: App {
@State private var appModel = AppModel.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(appModel)
}
}
}
@Observable class AppModel {
static var shared = AppModel()
var selectedFolder: Folder? = nil
}
struct Folder: Hashable, Identifiable {
let id = UUID()
let name: String
}
struct ContentView: View {
@Environment(AppModel.self) private var appModel
var folders = [Folder(name: "Recents"), Folder(name: "Deleted"), Folder(name: "Custom")]
var body: some View {
NavigationSplitView {
SidebarView(appModel: appModel, folders: folders)
} detail: {
if let folder = appModel.selectedFolder {
Text(folder.name)
}
else {
Text("No selection")
}
}
}
}
struct SidebarView: View {
@Bindable var appModel: AppModel
var folders: [Folder]
var body: some View {
List(selection: $appModel.selectedFolder) {
ForEach(folders, id: \.self) { folder in
NavigationLink(value: folder) {
Text(folder.name)
}
}
}
}
}
To reproduce the bug, just tap on one of the item.
Oddly enough, this works fine in the Simulator.
macOS 14 beta 5 is not affected either.
Apple folks: FB12981860