標籤:
轉: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.
(weak和strong)不同的是 當一個對象不再有strong類型的指標指向它的時候 它會被釋放 ,即使還有weak型指標指向它。
一旦最後一個strong型指標離去 ,這個對象將被釋放,所有剩餘的weak型指標都將被清除。
可能有個例子形容是妥當的。
想象我們的對象是一條狗,狗想要跑掉(被釋放)。
strong型指標就像是栓住的狗。只要你用牽繩掛住狗,狗就不會跑掉。如果有5個人牽著一條狗(5個strong型指標指向1個對象),除非5個牽繩都脫落 ,否著狗是不會跑掉的。
weak型指標就像是一個小孩指著狗喊到:“看!一隻狗在那” 只要狗一直被栓著,小孩就能看到狗,(weak指標)會一直指向它。只要狗的牽繩脫落,狗就會跑掉,不管有多少小孩在看著它。
只要最後一個strong型指標不再指向對象,那麼對象就會被釋放,同時所有的weak型指標都將會被清除。
iOS strong 和weak的形象理解(真的很精典!)