qf--strong references to iOS, weak references, and strong,retain,copy,weak,assignd relationships

Source: Internet
Author: User

Strong references and weak references:

We already know that memory management in OC is done through "reference counters". The life cycle of an object depends on whether it is also referenced by another object (whether Retaincount=0). In some cases, however, we do not want the object's destruction time to be determined by other object references, but when the object should have been destroyed when it was destroyed. At this point, we have to introduce the concept of "strong references" and "weak references".

Strong reference: When the current object is referenced by another object, the retain operation is performed and the counter +1 is referenced. When retaincount=0, the object is destroyed. This is the default reference because we want to manage the memory of the object. (The default is a strong reference)

Weak reference: The current object's life cycle is not restricted by other object references, when it should be destroyed when it is destroyed. Even if its reference is not broken, it will be destroyed when its life cycle is reached.

When a property is defined, it is a strong reference if declared as a retain type, or a weak reference if declared as a assign type. Later, when the memory management is completed by arc, if a strong reference is made, it is declared as strong, and if it is a weak reference, it is declared as weak.

  therefore, retain and strong are consistent (declared as strong references); Assign and weak are basically consistent (declared as weak references). The reason that they are basically consistent is because they are still different, weak Strictly said should be called " zero Weak reference ", that is, when the object is destroyed, will automatically put its pointer to nil, so as to prevent the wild pointer error. and assign destroy the object will not put the object's pointer nil, the object has been destroyed, but the pointer is still in a silly point to it, which is a wild pointer, which is more dangerous.

Avoid the deadlock of "strong reference loops":

The default reference is a strong reference, but it says that sometimes we have to use weak references, what is the case?

An answer, a strong reference loop: A object strongly references a B object, and a B object 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".

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.

Summary: Due to memory management, the reference in OC is strongly referenced by default, but in order to avoid a "strong reference loop Deadlock", there is a weak reference (assign).

About copy: Reference links

Retain and strong are both pointer copies. when there are other objects referencing the current object, a copy of the current object's address is copied so that it also points to the current object. So, still the same object, just retaincount+1;

Copy is a copy of the content. is a real copy of a new object that copies its memory contents and becomes a new object (Retaincount=1).

Deep Copy (mutablecopy) and shallow copy (copy):

A deep copy is a copy of the content, and a shallow copy is a pointer copy.

In OC, to make a copy of an object, the class to which the object belongs must comply with the nscopying and nsmutablecopy protocols and override the Copywithzone: and Mutablecopywithzone: Methods. And the system native class, the reason can be copied directly because it has helped us to do these things automatically.

  

qf--strong references to iOS, weak references, and strong,retain,copy,weak,assignd relationships

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.