我正在学习 CoreData。我一直在经历 this tutorial但是在我的 UIViewController
方法之一中遇到了编译问题。除非我犯了一个明显的错误,这正是本教程中的代码所做的。我正在使用 Xcode 8.2.1。
func getStores() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Store")
do {
// Compilation error on the next line:
// Cannot convert value of type 'NSFetchRequest<Store>' to expected argument type 'NSFetchRequest<NSFetchRequestResult>
stores = try managedContext.fetch(fetchRequest)
} catch {
// handle error
}
}
}
解决方案:
早些时候,在我的 VC 中,我使用错误的 CoreData 实体声明了 stores
数组。修正该错误即可解决问题。
最佳答案
换行
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Store")
至:
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = Store.fetchRequest()
或者
let fetchRequest = NSFetchRequest<Store>(entityName: "Store")
关于ios - 如何在 NSManagedObjectContext 上正确调用 fetch()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42700323/