简体   繁体   中英

convertion of a class to arc from non-arc

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM