I have found two solutions to the problem:
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!)
and
let myClass = NSEntityDescription.insertNewObject(forEntityName: "MyClass", into: context) as! MyClass
but I cannot understand the difference in the end. And how does it affect NSManagedObjectContext?
The have the same effect. The method on NSEntityDescription is a "factory" method, which you don't see too often in Objective-C (that method existed before Swift did). But the end result is the same as using the designated initializer. Although the factory method's code is not available, you can assume that it calls the designated initializer at some point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With