No automatic synthesize

Discussion in 'iOS Programming' started by jcfiesta, Mar 23, 2013.

  1. jcfiesta macrumors newbie

    Joined:
    Mar 23, 2013
    #1
    Hi,

    I'm new here and new in studying iOS dev't.

    I have a Label and when I drag it to my ViewController header file it automatically creates a property. However, when I checked my implementation file there was no synthesize part. I thought it should be automatic? What do you think I'm doing wrong?
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    Because in the newest versions of Xcode, the @synthesize in the implementation file is not needed.
     
  3. jcfiesta thread starter macrumors newbie

    Joined:
    Mar 23, 2013
    #3
    If that's the case, how come my property cannot be recognize in the implementation code?

    ----------

    Oh wait. I think I get it now. All I need to do is reference it with an underscore prefix.
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    Sort of, but no.

    Using the underscore lets you access the underlying auto-created instance variable. Don't do that.

    Instead, use the property:

    Code:
    [self myProperty]
    
    or

    Code:
    self.myProperty
    There are limited times when you should use the iVar directly (custom getters and setters and possibly in the dealloc method).
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    1. Post your code. Enough of it so someone else can compile it.

    2. Post the error message. Copy and paste the complete actual text, and identify where it occurs in the source.

    3. Post your OS and Xcode versions.
     
  6. jcfiesta thread starter macrumors newbie

    Joined:
    Mar 23, 2013
    #6
    why do we need to reference it to self if we are in the class itself? kindly clarify. thanks!

     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Because if you don't, you are circumventing the accessors (getters & setters). You may not understand these terms right now, but such circumventions can have implications with subclassing, KVO and atomicity.

    Or, if you've set up a custom getter (for example: to lazy-load some value into the instance variable and that operation is expensive), accessing the instance variable directly may not contain any value, whereas the custom getter can guarantee one.
     
  8. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #8
    What he said.

    Basically, until you fully understand the difference between using an ivar and property, you should use the property. The only time you should access your ivars directly, until you get more advanced, is in the code of a custom getter/setter, or possibly in your dealloc method.
     

Share This Page