The difference between copy,retain,strong,assign,weak in iOS and the use

Source: Internet
Author: User

Using assign: for underlying data types (Nsinteger) and C data types (int, float, double, char, etc.)
Using copy: On NSString
Use retain: For other nsobject and its subclasses

Assign is a simple assignment that does not change the reference count, so use the underlying data directly

Copy is a direct copy of the content and becomes a new object.

Retain frees the old object, assigns the value of the old object to the new object, and adds 1 to the reference count of the new object

Retain is a pointer copy copy, which allocates new memory to the content copy and releases the old object before copying.

In Arc, retain and strong are consistent (declared as strong references); Assign and weak are basically the same (declared as weak references)

======= Weak references (weak,assign), and the difference =======================================

The reason for the weak reference: to prevent cyclic application.

Strong reference loops: The A object strongly references the B object, and the B object also strongly references a. Because both are strong references, that is, whether a is B is to be in the other side of the reference is broken before it can be destroyed, but to break the reference, you must destroy the object. There will be such an impasse, in order to avoid this situation, there should be an object "weakness" to make it a "weak reference". The difference between weak and assign is that when the object is destroyed, weak will put the pointer to nil, and assign will not, so assign will cause wild pointers, more dangerous.

More commonly, a reference between a parent-child view in a view that is strongly referencing a child view, and a child view that has a weak reference to the parent view.

= = =Atomic and nonatomic====================================================

Atomic is a thread-protection technique used by OBJC, basically to prevent the data from being read by another thread when the write is not completed. This mechanism is resource-intensive, so nonatomic is a very good choice on small devices such as the iphone, if there is no communication programming between multiple threads.

The difference between copy,retain,strong,assign,weak in iOS and the use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.