In Obj-C, properties can be configured to be weak/strong. Instance variables. like following -
@interface MyClass {
NSObject *a;
}
Does the MyClass's object keep weak reference to a
or strong, or something else? I think iVar is not released until its object is released. Why don't we specify weak/strong for iVar like properties?
对ivar的默认引用是__strong
,但您可以将其显式设置为__weak
或__strong
You question inspired me and i did a deep search on ObjectiveC memory management. I would like to share something with you that I got from 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?
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.If you don't want a variable to maintain a strong reference, you can declare it as __weak, like this:
NSObject * __weak weakVariable;
@interface MyClass {
__weak NSObject *a;
__strong NSObject *a;
__unsafe_unretained NSObject *obj;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.