Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

must call a designated initializer of the superclass NSManagedObject - in swift

I get the compiler message: must call a designated initializer of the superclass NSManagedObject (in swift)

//-------------------------------------
class abc : NSManagedObject {
    init(x:String, y:String){
        super.init()      // <<====== here!!
        self.x = x
        self.y = y
    }
}
//-------------------------------------

the var(s) are declared in the extension xxxx { .... } How to initialize this superclass?

like image 905
Neu4Swift Avatar asked Mar 18 '16 00:03

Neu4Swift


2 Answers

The designated initialiser is

init(entity entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)

and that is the super init function you must call.

like image 167
Wain Avatar answered Oct 02 '22 21:10

Wain


In Xcode Version 12.5 (12E262) I would do it like this this:

class abc : NSManagedObject {
    init(x:String, y:String , entity: NSEntityDescription, context: NSManagedObjectContext?){
        super.init(entity: entity, insertInto: context)
        self.x = x
        self.y = y
    }
}
like image 44
charelf Avatar answered Oct 02 '22 23:10

charelf