我在项目中实现coredata时遇到问题。它似乎能够保存但不能获取。这里的代码CoreData实现swift 3
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Utente", in: context)
let item = NSManagedObject(entity: entity!, insertInto: context)
var utente: Profilo? = nil
let vc = CustomTabBarController() as UIViewController
let vc1 = LoginController() as UIViewController
// Configure Fetch Request
do {
let request: NSFetchRequest<NSFetchRequestResult> = Profilo.fetchRequest()
let result = try context.fetch(request) as Profilo
utente = result as Profilo
print()
if utente?.type != nil {
if utente?.type == "students"{
print("students")
present(vc, animated: true, completion: nil)
}
if utente?.type == "parents"{
print("parents")
present(vc, animated: true, completion: nil)
}
if utente?.type == "teachers"{
print("teachers")
present(vc, animated: true, completion: nil)
}
} else {
print("variable type empty")
present(vc1, animated: true, completion: nil)
}
} catch {
let fetchError = error as NSError
print(fetchError)
}
我也得到结果行错误: 不能援引“取”与类型(NSFetchRequest)的参数列表
你可以发布你的保存到核心数据请,我自己有麻烦。谢谢 ! – user3739902
我刚刚复制下面的答案,但现在它不工作,因为我改变了模型。我搞清楚如何使它再次工作 –
我设法使我的coredata工作,所以如果你想我可以在这里发布它作为答案。 – user3739902