Error with Swift and Core Data: fatal error: use of unimplemented initializer 'init(entity:insertIntoManagedObjectContext:)'

前端 未结 2 503
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 04:26

I have the following class that inherits from NSManagedObject:

import Foundation
import CoreData


class Note: NSManagedObject {



    @NSManag         


        
2条回答
  •  执念已碎
    2021-01-02 04:34

    This is documented behavior.

    Swift subclasses do not inherit their superclass initializers by default

    For example, following code does not even compile, because Child does not inherit init(id:String) automatically. This mechanism make sure name in Child class properly initialized.

    class Parent {
        var id:String
        init(id:String) {
            self.id = id
        }
    }
    
    class Child:Parent {
        var name:String
        init(id:String, name:String) {
            self.name = name
            super.init(id: id)
        }
    }
    
    var child1 = Child(id:"child1")
    

    If you define only convenience initializers in subclass, then it automatically inherits all of its superclass designated initializers as documented in "Automatic Initializer Inheritance" section

提交回复
热议问题