简体   繁体   English

NSManagedObject 的两个初始值设定项有什么区别

[英]What is the difference between two initializers of NSManagedObject

If somebody could please help me on what is the difference on creating an object of NSManagedObject subclass from these initialization methods:如果有人可以帮助我从这些初始化方法创建 NSManagedObject 子类的对象有什么区别:

init(context:) 初始化(上下文:)

vs对比

init(entity:insertInto:) 初始化(实体:插入:)

Thanks.谢谢。

It's about implicitly and explicitly specifying entity name so这是关于隐式和显式指定实体名称,所以

guard let appDelegate =
    UIApplication.shared.delegate as? AppDelegate else {
        return
}

let managedContext =
    appDelegate.persistentContainer.viewContext

1- This will create an object from entity named Person represented by class CustomObj and inserts it in managedContext 1- 这将从由类CustomObj表示的名为Person实体创建一个对象,并将其插入到managedContext

let entity =
    NSEntityDescription.entity(forEntityName: "Person",
                               in: managedContext)!

let person1 = CustomObj(entity: entity, insertInto: managedContext)

2- This will create an object from class CustomObj where entity name is CustomObj and inserts it in managedContext 2- 这将从类CustomObj创建一个对象,其中实体名称是CustomObj并将其插入到managedContext

let person2 = CustomObj(context: managedContext)

class CustomObj:NSManagedObject {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM