【发布时间】:2017-05-16 18:17:23
【问题描述】:
我已将上一个故事板中的用户输入名称保存到实体“UserInfo”下的 xcdatamodel 中,名称为“name”。我试图在下一个故事板中获取它以显示在标签中以问候用户。我收到错误“无法调用类型为 'NSFetchRequet' 的初始化程序,其参数类型为 '(entityName:String, attributeName: String)”
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
//getting the managed context where the entity we need is
let managedContext = appDelegate.persistentContainer.viewContext
//make fetch request
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "UserInfo", attributeName: "name")
//try to fetch the entity we need, else print error
do {
Username = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
【问题讨论】:
标签: swift core-data fetch nsfetchrequest