KVO (key / value monitoring) can do this, sort of, but it will be even more code and probably won't be easier to read or write.
You may be familiar with KVO, but in case you (or others) are not: in your init function, you will do the following:
[self addObserver:self forKeyPath:@"date" options:0 context:NULL];
Then you would do this:
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context { if (object == self && [keyPath isEqualToString:@"date"]) {
Finally, in dealloc you will do the following:
[self removeObserver:self forKeyPath:@"date"];
As you can see, this is even more code and harder to understand. Not very effective for someone whose goal should be lazy :-) But KVO is the main function of Objective-C data binding. There are some platforms (such as Flex) that can bind data with significantly less code, but a lot of work is required in Objective-C.
By the way, it doesnβt matter, but the example code that you showed is buggy - probably it should look something like this:
- (void) setDate:(NSDate *)newDate { if (date != newDate) { [date release]; date = [newDate retain];
source share