ARC时代的财产与ivar [英] Property vs. ivar in times of ARC

查看:75
本文介绍了ARC时代的财产与ivar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,设置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?

推荐答案

如果是变量:

  1. 使用 ARC KVO
  2. 不需要任何自定义getter/setter.
  1. Is declared in a class using ARC.
  2. Is used solely for class implementation (not exposed as part of the class interface).
  3. Does not require any KVO.
  4. 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, or 0 if it is a primitive.
    • 这篇关于ARC时代的财产与ivar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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