我的变量entityDescription似乎为空,所以我在编译时发生了致命错误。有人知道解决方案吗?在同一个类(AppDelegate)中声明了persistentContainer。
let entityDescription = NSEntityDescription.entity(forEntityName: "Person", in: self.persistentContainer.viewContext)
let newPerson = NSManagedObject(entity: entityDescription!, insertInto: self.persistentContainer.viewContext)
newPerson.setValue("Thomas", forKey: "first")发布于 2016-07-13 14:31:47
请检查方法名称,下面的代码为我创建了一个NSEntityDescription对象。
let desc:NSEntityDescription? = NSEntityDescription.entityForName("Person", inManagedObjectContext: self.managedObjectContext);
print(desc)发布于 2016-07-13 14:18:52
您应该看看Core Data Xcode模板。(选择新建项目、检查、主/详细信息、检查核心数据。)在Swift 3中,您可以通过使用有效的托管对象上下文创建一个新对象。
let newPerson = Person(context: context)
newPerson.first = "Thomas"https://stackoverflow.com/questions/38339167
复制相似问题