Here is 2 code snapshot-
@interface A:NSObject
{
NSMutableArray *a;
}
@property (retain) NSMutableArray *a;
@implementation
@synthesize a;
-(id)init
{
if(self=[super init])
{
a=[[NSMutableArray alloc] init];
}
}
@end
@interface A:NSObject
{
NSMutableArray *_a;
}
@property (retain) NSMutableArray *a;
@implementation
@synthesize a=_a;
-(id)init
{
if(self=[super init])
{
_a=[[NSMutableArray alloc] init];
}
}
@end
Now what i need to know, is in both code instance variable assigned value directly rather than using accessor and retain count is 1? Or there is difference between them. Thanks.
And one more things, apple recommended not to use accessor in init/dealloc, but at the same time ask not to directly set iVar. So what is the best way to assign value of ivar in init()??
ARC vs non-ARC
First, you should decide whether you want to use automatic reference counting (ARC) or manual reference counting. It looks like you’ve chosen the latter – which is fine because you can always transition to ARC later.
Property attributes/setter semantics
You have marked the setter semantics for the property
aasretain. This will work; but since the property is exposed in the class interface, class users may use it to set the_aivar. Since class users may not expect your classAto directly modify the theNSMutableArrayinstance they passed, better to usecopysemantics for mutable object properties.Property access in
initanddeallocUse of declared properties in
initanddeallocis often discouraged because doing so may have side effects due to KVO. These side effects may not work well with a partially constructed or partially deconstructed object.Your questions
Here’s what I would do:
EDIT:
Or, if auto synthesize is not available: