【发布时间】:2023-04-02 00:08:01
【问题描述】:
我在项目中实现 coredata 时遇到问题。它似乎能够保存但不能获取。这是代码
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) 的参数列表调用“获取”
【问题讨论】:
-
您能否将您的存档发布到核心数据中,我自己也遇到了麻烦。谢谢!
-
我刚刚复制了下面的答案,但现在它不起作用,因为我改变了模型。我正在研究如何让它再次工作
-
我设法让我的核心数据工作,所以如果你愿意,我可以在这里发布它作为答案。
-
那太好了,谢谢!!!!
-
我在下面发布了我的代码作为答案:)
标签: swift core-data nsfetchrequest