ios - An Xcode macro for creating property setters -
I have a class with many properties when each property is set, I need to update the UI of my application is needed. Sets look something like this:
@ synthesis tracklightlight color = _trackHighlightColour; - (zero) setTrackHighlightColour: (UIColor *) trackHighlightColour {_trackHighlightColour = trackHighlightColour; [Self update UI]; } Instead of typing it 10 times, I would like to use the macro. This is what I have done so far:
#if! Defined (PROPERTY_SETTER) #define PROPERTY_SETTER (PROPERTY_NAME, UPPER_PROPERTY_NAME) @ synthesis (PROPERTY_NAME) = _ (PROPERTY_NAME); \ \ - (zero) set (UPPER_PROPERTY_NAME): (UIColor *) (PROPERTY_NAME) \ {\ _ (PROPERTY_NAME) = (PROPERTY_NAME); \ [Self update UI]; \} #endif Unfortunately, this is something problems.
- Required to repeat (miner) property with repeat name
-
The compiler does not allow me to name the property, instead I have to pass it one As a string:
@implementation FooClass
PROPERTY_SETTER (@ "trackHighlightColour", "TrackHighlightColour");
@end
It feels very close to leaving this technique.
This is what I am using with MRC
#define GENERATE_RETAIN_SETTER (ATTRIBUTE_NAME, UPDATER) \ if (_ ## ATTRIBUTE_NAME! = ATTRIBUTE_NAME) {\ # [## ATTRIBUTE_NAME release]; \ _ ## ATRIBUTEN = [ATIIUUUTUAI]; \ \ [Auto update]; \}
- (zero) set color: (UIColor *) color {GENERATE_PROPERTY_SETTER (color, updateUI)} - (zero) set collar: (UIColor *) color {if (_color! = Color) {[_color release]; _color = [color intact]; [Self update UI]; }} This can be changed for your specific needs but there is no way to capitalize the string in the preprocessor. With the latest compiler, you can lose the @ synthesis , but to actually do what you want, you can create a macro with four parameters < Code> #define GENERATE_RETAIN_SETTER (PROPERTY, TYPE, SETTER, UPDATER) \ @ synthesize PROPERTY = _ ## PROPERTY; \ \ - (zero) SETTER: (TYPE *) property {\ if (_ ## property! = Property) {\ [_ ## property release]; \ _ ## property = [retaining property]; \ \ [Auto update]; Using
GENERATE_RETAIN_SETTER (Color, UIColor, setColor, updateUI) You should meet @ synthesis color = _color; - (zero) set collar: (UIColor *) color {if (_color! = Color) {[_color release]; _color = [color intact]; [Self update UI]; }}
Comments
Post a Comment