Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convertion of a class to arc from non-arc

Tags:

ios

iphone

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.

like image 489
Lochana Ragupathy Avatar asked Dec 14 '25 10:12

Lochana Ragupathy


2 Answers

_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.

EDIT: After seeing the comment

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.

like image 104
Krishnabhadra Avatar answered Dec 16 '25 00:12

Krishnabhadra


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.
like image 26
Midhun MP Avatar answered Dec 16 '25 00:12

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!