Is it possible to customize @synthesized properties?
Posted
by
Dan K.
on Stack Overflow
See other posts from Stack Overflow
or by Dan K.
Published on 2011-01-13T06:03:47Z
Indexed on
2011/01/13
6:53 UTC
Read the original article
Hit count: 213
I'm probably just being a bit lazy here, but bear with me. Here's my situation. I have a class with two nonatomic, retained properties. Let's say:
@property (nonatomic, retain) UITextField *dateField;
@property (nonatomic, retain) NSDate *date;
I synthesize them as expected in the implementation. What I want to happen is that whenever the setter on date is invoked, it also does something to the dateField (i.e. it sets the text property on the dateField to be a nicely formatted version of the date).
I realize I can just manually override the setter for date in my implementation by doing the following:
- (void) setDate:(NSDate *)newDate {
if (date != newDate) {
[date release];
date = [newDate retain];
// my code to touch the dateField goes here
}
}
What would be awesome is if I could let Objective C handle the retain/release cycle, but still be able to "register" (for lack of a better term) a custom handler that would be invoked after the retain/release/set happens. My guess is that isn't possible. My google-fu didn't come up with any answer to this, though, so I thought I'd ask.
© Stack Overflow or respective owner