在下面的代码中我无法选择任何列表项:
import SwiftUI
import SwiftData
struct ItemView: View {
@Environment(\.modelContext) private var context
@Query(sort: \Item.title) private var items: [Item]
@State private var selection: String? = nil
var body: some View {
if items.isEmpty {
ContentUnavailableView("Enter your first item.", systemImage: "bookmark.fill")
} else {
List(items, id: \.self, selection: $selection) { item in
Text(item.title)
}
}
}
}
但是,在一个非常相似的示例中,该示例使用数组作为列表项而不是数据库,我可以选择项目:
import SwiftUI
struct ContentView: View {
@State private var selection: String?
@State private var isOn: Bool = false
let names = [
"Cyril",
"Lana",
"Mallory",
"Sterling"
]
var body: some View {
NavigationStack {
List(names, id: \.self, selection: $selection) { name in
Toggle(isOn: $isOn) {
Text(name)
}
}
.navigationTitle("List Selection")
.toolbar {
}
}
}
}
我的控制台日志显示:
Can't find or decode reasons
Failed to get or decode unavailable reasons
NSBundle file:///System/Library/PrivateFrameworks/MetalTools.framework/ principal class is nil because all fallbacks have failed
我做错什么了?