【发布时间】:2016-12-08 15:49:27
【问题描述】:
我找到了两个解决问题的方法:
public class MyClass: NSManagedObject {
init(_ entity:NSEntityDescription, dict: NSDictionary, context: NSManagedObjectContext) {
super.init(entity: entity , insertInto: context)
}
}
let entity = NSEntityDescription.entity(forEntityName: "MyClass", in: self.context!)
_ = MyClass.init(entity!, dict: item as! NSDictionary, context: self.context!)
和
let myClass = NSEntityDescription.insertNewObject(forEntityName: "MyClass", into: context) as! MyClass
但我无法理解最终的区别。又对NSManagedObjectContext有何影响?
【问题讨论】:
-
我不知道第一个版本,但我为您准备了第三个版本:
let createe = MyClass(context: context),我更愿意避免知道实体名称的需要。 -
official doc here 确实提到了 insertNewObject 和 init 的关系
标签: ios objective-c swift core-data nsmanagedobjectcontext