Difference between strong, weak, copy, assign in iOS

1: In the ARC environment, strong replaces retain.weak instead of assign
2: The role of weak: In the ARC environment, all weak pointers to this object will be set to nil. This T feature is very useful. I believe many developers have been troubled by EXC_BAD_ACCESS caused by pointers pointing to released objects. After using ARC, no matter whether it is a strong or weak type pointer, it will no longer point to a destroyed object. From Fundamentally solve the crash caused by accidental release.

 3: The role of assign: simple assignment, does not change the reference count, for basic data types (such as NSInteger, CGFloat) and C data types (int, float, double, char, etc.) for simple data types 
4: The role of copy: establish An object with an index count of 1, and then release the old object 
5: The role of strong: In the ARC environment, as long as an object is pointed to by a strong pointer, the object will not be destroyed. If the object is not pointed to by any strong pointer, it will be destroyed. By default, all instance variables and local variables are of type strong. It can be said that the behavior of strong type pointers is similar to that of retain under non-ARC. 

6: The role of retain: In the non-ARC era, you need to retain an object you want to keep yourself, which is not needed in the ARC environment. The only thing to do now is to use a pointer to point to the object, and the object will stay on the heap as long as the pointer isn't reset to null. When the pointer points to the new value, the original object is released once. This is useful for instance variables, variables of sunthesize or local variables.

basically:

    Use copy for NSString classes, weak for control classes, strong for complex data classes, and assign for digital classes, such as INUIgter, nsinter, and cgreck.

 

     In particular, it is recommended to use weak for the control dragged in the xib/SB to connect to the code. The system is also created natively (weak, nonatomic). Don't change it, know why not? Because after the control is dragged to the xib/SB, the system automatically assigns it strong, so drag it to the code and use weak. If you don't believe me, right click xib/SB -> view it in XML format and you will understand everything

 

     If an object is loaded repeatedly for a certain period of time, and you do not want to re-alloc every time it is loaded, then strong, strong guarantees to maintain a strong reference to this object, as long as there is 1 strong reference to this object , then it will not be released, of course, multiple strong acting on it at the same time will not release it.

If an object is loaded only once in a certain period of time, and it is determined that it is no longer used after loading, then weak can be used, so that when the reference count is decreased by 1 for other reasons (such as removefromsuperview), the object is automatically released. There is no need to release it again in delloc, but you must ensure that the object is not used again after release, otherwise it will cause errors.

In fact, the functions of strong and retain are somewhat similar, except that strong and weak are introduced in arc, and they are regarded as A pair, the correspondence is somewhat similar to retain and assign 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325286792&siteId=291194637