ARC时代的财产与ivar [英] Property vs. ivar in times of ARC
问题描述
据我了解,设置ivar现在会保留分配给它的对象,因为设置变量默认为strong
限定符.因为ivars在它们声明的对象的范围内,并且strong
将对象保留在变量的范围内,所以这意味着在包含ivar的对象仍然存在的情况下,永远不会释放ivars值.
It is my understanding that setting an ivar now retains the object being assigned to it, since setting variables defaults to the strong
qualifier. Because ivars are in the scope of the object they are declared in and strong
retains objects within the scope of the variable, this means the ivars value would never be released while the object containing the ivar is still alive.
这正确吗?
如果是这样,我是否认为内存管理在保留(强)属性和简单的ivar之间没有区别?
If so, am I right in thinking that there is, in terms of memory management, no difference between a retaining (strong) property and a simple ivar anymore?
推荐答案
如果是变量:
- Is declared in a class using ARC.
- Is used solely for class implementation (not exposed as part of the class interface).
- Does not require any KVO.
- Does not require any custom getter/setter.
然后将其声明为没有相应的@property
/@synthesize
的ivar,并在实现中直接引用它是适当的.它与封装内联,以在类实现文件中声明此ivar.
Then it is appropriate to declare it as an ivar without a corresponding @property
/@synthesize
, and to refer to it directly within the implementation. It is inline with Encapsulation to declare this ivar in the class implementation file.
// MyClass.h
@interface MyClass : ParentClass
@end
// MyClass.m
@implementation MyClass {
NSString *myString;
}
- (void)myMethod {
myString = @"I'm setting my ivar directly";
}
@end
- 此ivar将被ARC编译器视为
__strong
. - 如果它是对象,则将其初始化为
nil
;如果它是基元,则将其初始化为0
. - This ivar will be treated as
__strong
by the ARC compiler. - It will be initialized to
nil
if it is an object, or0
if it is a primitive.
这篇关于ARC时代的财产与ivar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!