【发布时间】:2017-03-09 16:18:20
【问题描述】:
我正在学习 CoreData。我一直在经历this tutorial,但在我的UIViewControllers 方法之一中遇到了编译问题。除非我犯了一个明显的错误,这正是教程中的代码所做的。我正在使用 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 数组。修正这个错误解决了问题。
【问题讨论】:
-
如果
stores定义为[Store]类型,那么您可能会与对fetch的调用不匹配,在您的情况下,我认为它的类型为[NSManagedObject]- 什么是您收到的实际错误消息?
标签: ios swift core-data nsfetchrequest