[英]iVars references strong, weak or what?
In Obj-C, properties can be configured to be weak/strong. 在Obj-C中,属性可以配置为弱/强。 Instance variables.
实例变量。 like following -
喜欢以下 -
@interface MyClass {
NSObject *a;
}
Does the MyClass's object keep weak reference to a
or strong, or something else? 请问MyClass的的对象保持弱引用
a
或强,或其他什么东西? I think iVar is not released until its object is released. 我认为iVar在其对象发布之前不会发布。 Why don't we specify weak/strong for iVar like properties?
为什么我们不为iVar属性指定弱/强?
对ivar的默认引用是__strong
,但您可以将其显式设置为__weak
或__strong
You question inspired me and i did a deep search on ObjectiveC memory management. 您的问题激发了我的灵感,并对ObjectiveC内存管理进行了深入研究。 I would like to share something with you that I got from Apple Doc.
我想与你分享一些我从Apple Doc获得的东西。
Default behavior of instance variable 实例变量的默认行为
Instance variable maintains a strong reference to the objects by default
默认情况下,实例变量保持对对象的强引用
Why don't we specify weak/strong for iVar like properties? 为什么我们不为iVar属性指定弱/强?
Local variables and non-property instance variables maintain a strong references to objects by default.
默认情况下,局部变量和非属性实例变量保持对对象的强引用。 There's no need to specify the strong attribute explicitly, because it is the default.
没有必要明确指定强属性,因为它是默认属性。
A variable maintains a strong reference to an object only as long as that variable is in scope, or until it is reassigned to another object or nil.只要该变量在范围内,或者直到将其重新分配给另一个对象或nil,变量就会保持对对象的强引用。
If you don't want a variable to maintain a strong reference, you can declare it as __weak, like this:
如果您不希望变量维护强引用,可以将其声明为__weak,如下所示:
NSObject * __weak weakVariable;
@interface MyClass {
__weak NSObject *a;
__strong NSObject *a;
__unsafe_unretained NSObject *obj;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.