码迷,mamicode.com
首页 > 移动开发 > 详细

转载:stackoverflow上对iOS中strong and weak的形象描述

时间:2015-03-30 10:51:50      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5

 

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.

转载:stackoverflow上对iOS中strong and weak的形象描述

标签:

原文地址:http://www.cnblogs.com/ooexception/p/4377256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!