Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
What is the difference when I use @property and @synthesize in first case, and @property and @synthesize and also declare same instance variable? Because both seem to work.
Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
What is the difference when I use @property and @synthesize in first case, and @property and @synthesize and also declare same instance variable? Because both seem to work.
There is no difference: the instance variable that you declare will be used, and its ARC properties will be overridden by these of your declared @property
.
A property's specified ownership is preserved in its metadata, but otherwise the meaning is purely conventional unless the property is synthesized. If a property is synthesized, then the associated instance variable is the instance variable which is named, possibly implicitly, by the @synthesize declaration. If the associated instance variable already exists, then its ownership qualification must equal the ownership of the property; otherwise, the instance variable is created with that ownership qualification.
Declaring an instance variable for a property that you intend to @synthesize
is best avoided. If you prefer an instance variable with a different name, you can use the synthesize syntax with the ivar name:
@synthesize myproperty = _myivar;
Both do work - however, on newer architectures if you declare a property, you don't need to specifically declare an iVar, because it is done for you.
It is preference, really - however, make sure that if you declare both, you realize when you are accessing the iVar directly, or going through the getters and setters.
This is a matter of personal preference, but I'd advise against explicitly declaring ivars. I've seen situations here on SO where people have done things like:
@interface MyClass
{
NSString *var;
}
@property (nonatomic, strong) NSString *var;
@end
And then
@implementation MyClass
@synthesize var = _var;
Thus, the user had explicitly declared both a property and an ivar of the same name, but then synthesized the property generating another ivar (in this example, one with the preceding underscore in the name), resulting in two ivars being generated. They then wonder why their explicitly declared ivar doesn't work. If you don't don't explicitly declare your ivar, you don't have this problem of accidentally ending up with two ivars because you happened to mistype the name in either the ivar explicit declaration or the @synthesize
statement.
Just declare only the property and then @synthesize
the ivar using another name (generally with the preceding underscore), and it is unambiguous (i.e. you are less likely to reference your ivar accidentally if you forget to reference the property var
properly as self.var
) and you never have to worry, e.g.:
@interface MyClass
@property (nonatomic, strong) NSString *var;
@end
And then
@implementation MyClass
@synthesize var = _var;