如何在Swift中为NSManagedObject子类创建指定的初始值设定项?

时间:2014-10-17 15:27:26

标签: ios core-data swift nsmanagedobject

class Alternative: NSManagedObject {

    @NSManaged var text: String
    @NSManaged var isCorrect: Bool
    @NSManaged var image: NSData
} 

convenience init(text: String, isCorrect: Bool, entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext!) {
        let alternative = Alternative(entity: entity, insertIntoManagedObjectContext: context) as Alternative
        alternative.text = text
        alternative.isCorrect = isCorrect
        return alternative
}

我想制作一个让我用这个调用初始化新对象的方法:

let newAlternative = Alternative("third platform", True, entityDescription, managedObjectContext)

但我得到错误:

Convenience initializer for Alternative must delegate with self.init

我需要在initalizer中更改以使我的示例用法有效吗?

4 个答案:

答案 0 :(得分:36)

便捷初始化程序必须在self上调用指定的初始化程序:

convenience init(text: String, isCorrect: Bool, entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext!) {
    self.init(entity: entity, insertIntoManagedObjectContext: context)
    self.text = text
    self.isCorrect = isCorrect
}

将被称为

let newAlternative = Alternative(text: "third platform", isCorrect: true,
     entity: entityDescription, insertIntoManagedObjectContext: managedObjectContext)

此外,您还可以将实体描述的创建移动到便利初始化器中,而不是将其作为参数传递(由 Mundi的答案):

convenience init(text: String, isCorrect: Bool, insertIntoManagedObjectContext context: NSManagedObjectContext!) {
    let entity = NSEntityDescription.entityForName("Alternative", inManagedObjectContext: context)!
    self.init(entity: entity, insertIntoManagedObjectContext: context)
    self.text = text
    self.isCorrect = isCorrect
}

答案 1 :(得分:10)

我只是用类函数做到了这一点:

class func newInstance(text: String, notes:String, 
                    context: NSManagedObjectContext) -> Item {
    var item = NSEntityDescription.insertNewObjectForEntityForName("Item", 
               inManagedObjectContext: context) as Item
    item.notes = notes
    item.text = text
    return item
}

你可以像这样打电话(几乎一样漂亮):

let item = Item.newInstance(text, notes:notes, context:context)

答案 2 :(得分:2)

您必须从便捷初始化程序中调用指定的初始值设定项。 此外,您不会从任何初始化程序返回任何内容。

要完成Apple的Swift文档中描述的规则,首先需要为子类指定初始化程序,调用其超类的init(),然后您可以提供仅允许的便利初始化程序从类声明中调用指定的初始值设定项。

这样可以:(更新:考虑到运行时自动初始化了标有@NSManaged的核心数据属性。感谢@Martin R)

init(text: String, isCorrect: Bool, image: NSData, entity: NSEntityDescription,   insertIntoManagedObjectContext context: NSManagedObjectContext!) {
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}

convenience init(text: String, isCorrect: Bool, entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext!) {
    self.init(text: text, isCorrect: isCorrect, entity: entity, insertIntoManagedObjectContext: context)
}

答案 3 :(得分:2)

Swift 3.1 解决方案:

convenience init(text: String, isCorrect: Bool, image: NSData, moc: NSManagedObjectContext) {
        let entity = NSEntityDescription.entity(forEntityName: "Alternative", in: moc)
        self.init(entity: entity!, insertInto: moc)
        // vars
        self.text = text
        self.isCorrect = isCorrect
        self.image = image
}