下面的代码是我的观点,我正在处理核心数据,但它一直给我错误,它无法在范围内找到实体,但应用程序运行良好,一切都被保存和获取得很好。
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(
entity: TestModelCoreData.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \TestModelCoreData.name, ascending: false)
]
) var entities: FetchedResults<TestModelCoreData>
var body: some View {
VStack {
Text("Hello, world!").padding()
Button(action: {
let newEntry = TestModelCoreData(context: self.moc)
newEntry.name = "New name"
if self.moc.hasChanges {
try? self.moc.save()
}
}) {
Text("Add entry")
}
List(entities, id: \.self) { entity in
Text(entity.name ?? "Unknown")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}