Suppose I have a property defined as @property (copy) NSString *name
.
Suppose I have a init
method defined as below.
-(instancetype) initWithName:(NSString *)name
{
self = [super init];
if (self)
{
_name = [name copy]; //Do I need this copy or will ARC do it automatically?
}
return self;
}
Do I need the copy in the commented line or will ARC handle it based on the copy
in the property declaration just like it would in the synthesized setter?
The copy
applies to the property's synthesized setter method, not the backing instance variable.
Since you are bypassing the property in the initWithName:
method and assigning directly to the instance variable, the copy
isn't applied automatically.
In order to honor the contract that the value is copied, you should explicitly call copy
when you directly assign a value to the instance variable. None of this has to do with ARC (or MRC). It's all about the contract you defined on the property.
tl;dr - yes, you should call copy
in the code you posted.