iOS5中强弱存储的说明 [英] Explanation of strong and weak storage in iOS5

查看:90
本文介绍了iOS5中强弱存储的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS5开发新手并使用objective-c。我无法理解存储之间的区别。我已经阅读了文档和其他SO问题,但它们听起来与我完全相同,没有进一步的见解。

I am new to iOS5 development and using objective-c. I have trouble understanding the difference between strong and weak storage. I have read the documentation and other SO questions, but they all sound identical to me with no further insight.

我读了文档:转换到ARC - 它引用了iOS4的保留,赋值和发布条款;这让我很困惑。然后我看看Open U CS193p,它区分强弱:

I read the documentation: Transitioning To ARC - it references to iOS4 terms of retain, assign, and release; which confuses me. Then I look into Open U CS193p, where it differentiates strong and weak:


:保持这个堆直到我不再指向它了

:只要别人强烈指出它就保持这个

Strong: "keep this in the heap until I don't point to it anymore"
Weak: "keep this as long as someone else points to it strongly"

两个定义是否相同=如果指针不再指向一个对象,那么释放持有该对象的内存?我理解指针,堆,分配或释放内存的概念 - 但强弱之间有什么区别?

Aren't the two definition identical = if pointer no longer pointing to an object, then free the memory holding the object? I understand the concept of pointers, heap, allocation or deallocation of memory - but what's the difference between strong and weak?

推荐答案

不同之处在于,只要没有指针,对象就会被解除分配它。即使弱指针指向它,一旦最后一个强指针消失,对象将被释放,所有剩余的弱指针将被清零。

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.

这篇关于iOS5中强弱存储的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆