objective-c - ARC时代的属性(property)与ivar

据我了解,现在设置 ivar 会保留分配给它的对象,因为设置变量默认为 strong 限定符。因为 ivars 在声明它们的对象的范围内,而 strong 将对象保留在变量的范围内,这意味着当包含 ivar 的对象仍然存在时,永远不会释放 ivars 值。

这对吗?

如果是这样,我是否认为在内存管理方面,保留(强)属性和简单 ivar 之间不再有区别?

最佳答案

如果是变量:

  1. 在使用 ARC 的类中声明.
  2. 仅用于类实现(不作为类接口(interface)的一部分公开)。
  3. 不需要任何 KVO .
  4. 不需要任何自定义 getter/setter。

然后将其声明为没有相应 @property/@synthesize 的 ivar 并在实现中直接引用它是合适的。它与 Encapsulation 内联在类实现文件中声明这个 ivar。

// MyClass.h
@interface MyClass : ParentClass
@end

// MyClass.m
@implementation MyClass {
    NSString *myString;
}

- (void)myMethod {
    myString = @"I'm setting my ivar directly";
}
@end
  • ARC 编译器会将这个 ivar 视为 __strong
  • 如果是对象则初始化为nil,如果是原语则初始化为0

https://stackoverflow.com/questions/7836182/

相关文章:

ios - 根据文本量更改 UITableViewCell 高度

iphone - 如何创建自定义键盘

ios - UICollectionView:如何检测滚动何时停止

objective-c - UIScrollView contentSize 不起作用

ios - performFetchWithCompletionHandler 永远不会被解雇

ios - iOS 10 中的错误 : Unable to copy asset informati

iphone - 如何使用 NSLog(@"Inside of the iPhone Simulat

ios - 在 iOS 上检测飞行模式

objective-c - 100% 不透明度 UILabel 超过 50% 不透明度背景(UIVi

ios - 更改 UITabBar 高度