Property vs. Property+Variable

Discussion in 'iOS Programming' started by Schnigges, Jun 2, 2011.

  1. Schnigges macrumors newbie

    Feb 22, 2010
    Hey guys,

    I've asked this question on several forums now, but couldn't get a satisfying answer.
    Having understood the main purpose of properties, which is the generation of getters / setters, and how to access them, I'm still struggling with the question of when it's best to have an instance variable AND a property, named the same or differently, and when you should just use a property OR a variable.
    For example, when you create a new project in Xcode and choose one of the templates (ViewBased, NavBased, etc. ) there is always JUST a window-property in the AppDelegate.h file and no instance variable.

    So...I do know that you access the property by using self.propertyName or by calling the name you choose in the @synthesize line, I just would like to understand when to use property + variable, and whether there are situations where it's more common to use property only OR variable only, because I can't believe this is kind of random...

    Best wishes,
  2. multinode macrumors regular

    Feb 4, 2011
    Interesting question ... I'd also like to know the answer. Can one of MR's wizards jump in here please.

  3. amorya macrumors 6502

    Jun 17, 2007
    If there's a property that you've set to synthesize, there will be an instance variable backing it. However, newer versions of the Objective-C runtime (which includes all versions on iOS, I think) will automatically generate that instance variable if you haven't created it manually.

    Personally, I tend to just create the property and don't touch the instance variable at all. Some people say that it's good practice to work with the instance variable directly in your dealloc method (to avoid side effects of any accessors you write in the future), but I've never ran into the problem so I don't bother.

Share This Page