Understanding of Strong and Weak in IOS development (87)

Source: Internet
Author: User

I think it is easy to understand

The difference is that an object will be deallocated as soon as there are no strong pointers to it. even if weak pointers point to it, once the last strong pointer is gone, the object will be deallocated, and all remaining weak pointers will be zeroed out.

 


Perhaps an example is in order.

 


Imagine our object is a dog, and that the dog wants to run away (be deallocated ).

 


Strong pointers are like a leash on the dog. as long as you have the leash attached to the dog, the dog will not run away. if five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.

 


Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog! "As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. as soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.

 


As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.

 


(Weak and strong) the difference is that when an object no longer has a strong pointer pointing to it, it will be released, even if there is a weak pointer pointing to it.

 


Once the last strong pointer leaves, the object will be released and all remaining weak pointers will be cleared.

 


It may be appropriate to give an example.

 


Imagine our object is a dog, and the dog wants to run away (be released ).

 


The strong pointer is like a dog. The dog will not run away as long as you hold the dog with a rope. If five people hold a dog (five strong pointers point to one object), the dog won't run unless the Five ropes fall off.

 


The weak pointer is like a child pointing to a dog and shouting, "Look! A dog is there. "As long as the dog is down, the child will be able to see the dog, which will always point to it. As long as the dog's rope falls off, the dog will run away, no matter how many children are watching it.

 


As long as the last strong pointer no longer points to the object, the object will be released and all weak pointers will be cleared.

 

 

 

 

 

The strong and weak keywords are added to attribute settings in iOS 5 to modify attributes (ARC is not supported before iOS 5)

Strong is used to modify attributes of strong references;

@ Property (strong) SomeClass * aObject;
Corresponds to the original
@ Property (retain) SomeClass * aObject; and @ property (copy) SomeClass * aObject;

Weak is used to modify attributes of weak references;
@ Property (weak) SomeClass * aObject;
Corresponds to the original
@ Property (assign) SomeClass * aObject;

_ Weak, _ strong is used to modify the variable, and _ unsafe_unretained and _ autoreleasing are used to modify the variable.
_ Strong is the default keyword.
_ Weak declares a weak reference that can be automatically nil.
_ Unsafe_unretained declares a weak application, but it will not be automatically nil. That is to say, if the memory area to which it is directed is released, this pointer is a wild pointer.
_ Autoreleasing is used to modify a function parameter, which is automatically released when the function returns.

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.