Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 579
Dan2552 Avatar asked Oct 29 '12 10:10

Dan2552


1 Answers

You can do with subclassing see the doc here

- (void)setName:(NSString *)newName
{
    [self willChangeValueForKey:@"name"];
    [self setPrimitiveName:newName];
    [self didChangeValueForKey:@"name"];
}
like image 193
jithinroy Avatar answered Sep 28 '22 02:09

jithinroy