Clarify @synthesize

Discussion in 'Mac Programming' started by linkedlist, Mar 7, 2010.

  1. linkedlist macrumors newbie

    Joined:
    Feb 27, 2010
    #1
    I am slowly working my way through a cocoa tutorial, some questions I have dont appear answered in the book.

    Just got to @property and @synthesize - I understand accessors and mutators and realise that this is what they are doing - but am I correct in thinking that if I use this method, all I will ever have is a very simple set and get mechanism?

    If I wanted to add anything else to the getter and setter, ie a message logged to the console when a value is set, then I have to explicitly type out the old set and get methods.

    There's no way to piggy back extra statements into the 'generated' methods is there?
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Unfortunately correct. This also means that the various attributes of properties ('atomic', 'copy', 'retain', etc...) are only automatic for @synthesized accessors. You'll need to ensure that the header matches the implementation for your own.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    The way I do this is through key-value observing. That way you keep the full implementation of @properties but still can do things after a value has changed. It's not as flexible though, so it depends on your situation, but has worked for me.
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Nobody forces you to use @synthesize. @synthesize tells the compiler "insert the code for me right here". Just write the _complete_ implementation of the accessor yourself.
     
  5. fritx macrumors newbie

    Joined:
    Aug 9, 2009

Share This Page