CoreData: error: Failed to call designated initializer on NSManagedObject custom class

前端 未结 2 836
眼角桃花
眼角桃花 2021-01-22 15:14

When I call the function CacheStation I get the error: CoreData: error: Failed to call designated initializer on NSManagedObject class SaveModel. Nothing more nothing less. How

2条回答
  •  难免孤独
    2021-01-22 15:37

    This line:

    let sm = SaveModel();
    

    uses the standard init() method to create an instance of SaveModel, but NSManagedObjects must be initialised using the designated initialiser:

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

    (or, as in your CacheStations method, using NSEntityDescription.insertNewObjectForEntityForName(_, inManagedObjectContext: moc) which calls the designated initialiser).

    Since it seems sm is created only to have an instance on which to call CacheStations, I would change that method to a class method:

    class func CacheStations(){
    

    and change your saveShizzle method to use the class method:

    @IBAction func saveShizzle(sender: AnyObject) {
        SaveModel.CacheStations();
    }
    

提交回复
热议问题