What is the difference in declaring variables.

Discussion in 'iOS Programming' started by BitcoSoftware, Dec 17, 2011.

  1. BitcoSoftware macrumors newbie

    Joined:
    Dec 17, 2011
    #1
    Can someone explain to me what the difference is and which is better to do?

    Code:
    @interface ViewReportController : UIViewController
    {
        UIPopoverController *pop;
    }
    
    @property (nonatomic, strong)UIPopoverController *pop;
    
    
    @end
    
    @implementation ViewReportController
    
    @synthesize pop;
    
    @end
    Code:
    @interface ViewReportController : UIViewController
    {
        UIPopoverController *_pop;
    }
    
    @property (nonatomic, strong)UIPopoverController *pop;
    
    
    @end
    
    @implementation ViewReportController
    
    @synthesize pop = _pop;
    
    @end

    Why do I see some code using the different @synthesize with two separate variables?
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    In both cases, you end up with a property named pop. In the first case, the instance variable that backs the property is also named pop (the default); in the second case the variable is named _pop. Technically the name of the instance variable doesn't have to be anything like the name of the property, but of course in practice it makes sense to make it something very similar.

    The reason to use the underscore like in the 2nd case is to make instance variables immediately recognizable in your code versus local variables, the latter of which you would presumably name without any special leading or trailing characters. Using the underscore also tends to help avoid naming conflicts with method parameters. Ultimately it is just a matter of style and has no real technical implications.

    Note that on platforms with a 64-bit runtime (e.g. iOS), you don't need the variable declaration as part of the interface; just declaring the property and synthesizing it will create the variable for you.
     
  3. BitcoSoftware thread starter macrumors newbie

    Joined:
    Dec 17, 2011
    #3
    Ok, that makes sense... so what is the difference of this:

    Code:
    @interface ViewReportController : UIViewController
    {
        UIPopoverController *pop;
    }
    
    
    and this:

    Code:
    @interface ViewReportController : UIViewController
    {
    
    }
    
    @property (nonatomic, strong)UIPopoverController *pop;
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The reason to name the ivar differently from the property is so that if you access the ivar directly by mistake the compiler will tell you.

    Code:
    pop = [[UIPopoverController alloc] init];
    will be a compiler error when you meant to say:

    Code:
    self.pop = [[[UIPopoverController alloc] init] autorelease];
     
  5. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    ALso, do you know what Synthesize does to your object?
     

Share This Page