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.

  1. Required to repeat (miner) property with repeat name
  2. 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

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -