Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BitcoSoftware

macrumors newbie
Original poster
Dec 17, 2011
2
0
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?
 
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.
 
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;
 
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];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.