iosobjective-cmogeneratorcopywithzone

copyWithZone using mogenerator


I'm using Mogenerator to generate my models. So in my Human model I have

- (id)copyWithZone:(NSZone *)zone
{
    AppointmentGrid *appointmentGridCopy = [[[self class] allocWithZone:zone] init];
    [appointmentGridCopy setEmployeeId:self.employeeId];
    [appointmentGridCopy setEmployeeObject:self.employeeObject];
    [appointmentGridCopy setServiceId:self.serviceId];
    [appointmentGridCopy setServiceObject:self.serviceObject];
    [appointmentGridCopy setStartTimestamp:self.startTimestamp];
    [appointmentGridCopy setEndTimestamp:self.endTimestamp];
    [appointmentGridCopy setAppointmentGridSlots:self.appointmentGridSlots];

    return appointmentGridCopy;
}

Since the Machine class has all the properties I haven't readded them into the Human file. However I get an error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppointmentGrid setEmployeeId:]: unrecognized selector sent to instance

Do I really need to redefine everything in my Human file?


Solution

  • Instances of NSManagedObject must be created using the designated initialiser

    initWithEntity:insertIntoManagedObjectContext:
    

    The Core Data property accessor methods are created dynamically at runtime, and that cannot work if the object was created with a plain init method.

    This might work (untested):

    AppointmentGrid *appointmentGridCopy = [[[self class] allocWithZone:zone] 
        initWithEntity:self.entity
        insertIntoManagedObjectContext:self.managedObjectContext];