I have a class with a number of properties. When each property is set I need to update the UI of my application. The setters look something like this:
@synthesize trackHighlightColour = _trackHighlightColour;
- (void)setTrackHighlightColour:(UIColor *)trackHighlightColour
{
_trackHighlightColour = trackHighlightColour;
[self updateUI];
}
Rather than type this out 10 times, I would like to use a macro. This is what I have so far:
#if !defined(PROPERTY_SETTER)
#define PROPERTY_SETTER(PROPERTY_NAME, UPPER_PROPERTY_NAME) @synthesize (PROPERTY_NAME) = _(PROPERTY_NAME);\
\
- (void)set(UPPER_PROPERTY_NAME):(UIColor *)(PROPERTY_NAME)\
{\
_(PROPERTY_NAME) = (PROPERTY_NAME);\
[self updateUI];\
}
#endif
Unfortunately this has a few problems.
- (Minor) The need to repeat the property name with both casing
The compiler does not allow me to pass the property name, instead I must pass it as a string:
@implementation FooClass
PROPERTY_SETTER(@"trackHighlightColour", "TrackHighlightColour");
@end
It feels too close to give up on this technique. Does anyone have any suggestions?