Reputation: 4320
Hi all,
@property (nonatomic, assign) NSInteger myindex;
this line changes to unsafe_unretained after performing a convertion to Objective C ARC can any one please explain me regarding this.
Upvotes: 3
Views: 516
Reputation: 107131
In your case it won't change to unsafe_unretained
because it is a scalar value.
Probably you wrote like:
@property (nonatomic, assign) NSInteger *myindex;
that's why it is converting to unsafe_unretained
.
In ARC assign
is effectively unsafe_unretained
.
For scalars values like int
, float
. You can use assign
itself.
For objects you can use either weak
or unsafe_unretained
, it depends on the context.
unsafe_unretained
and weak
prevent the retention of objects, but in slightly different ways.
weak
the pointer to an object will convert to nil when the object is deallocated.unsafe_unretained
will continue pointing to the memory where an object was, even after it was deallocated. This can lead to crashes due to accessing that deallocated object.Upvotes: 1
Reputation: 34275
_unsafe_unretained is a variable qualifier.
From transition to ARC documentation
__unsafe_unretained specifies a reference that does not keep the referenced object alive and is not set to nil when there are no strong references to the object. If the object it references is deallocated, the pointer is left dangling.
Simply, if you use this qualifier, it won't do any retain for you, and it won't automatically set its reference to nil, when there are no other object is retaining it.
First thing first, the variable in question is a primitive type (NSInteger
, like CGFloat
, float
etc). So variable qualifiers like __weak
, __strong
, __unsafe_unretained
has no effect on them. So there is no danger in below code.
@property (nonatomic, assign) NSInteger myindex;
Now if in some other parts, you have a non primitive type, like NSString, UIImage etc, with this qualifier, then you must make sure you retain the variable, throughout the time of your use, and release them after.
Upvotes: 2