Dan2552 Dan2552 - 3 months ago 21
iOS Question

Overriding property setter in a class category

I've got an

NSManagedObject
class that I want to override a setter to but I've been told it's good practice to not modify the automatically generated class file and create categories to instead extend them (because if you change the model and regenerate the file, you lose all your additions).

If I make a method for the setter in a category, it definitely runs the method (tested with NSLog), but I don't know how to assign the actual property value. Normally, I'd synthesise the property using

@synthesize finished = _finished;


so that I can access the property in the setter using
_finished
, like so:

- (void)setFinished:(NSNumber *)finishedValue {
_finished = finishedValue;
self.end_time = [NSDate date];
}


but where the property is defined in the
NSManagedObject
this doesn't seem possible.

Answer

You can do with subclassing see the doc here

- (void)setName:(NSString *)newName
{
    [self willChangeValueForKey:@"name"];
    [self setPrimitiveName:newName];
    [self didChangeValueForKey:@"name"];
}
Comments