【发布时间】:2011-08-04 07:52:51
【问题描述】:
有没有办法在 Objective C 中声明私有属性?目标是从合成的 getter 和 setter 中受益,实现某种内存管理方案,但不向公众公开。
尝试在类别中声明属性会导致错误:
@interface MyClass : NSObject {
NSArray *_someArray;
}
...
@end
@interface MyClass (private)
@property (nonatomic, retain) NSArray *someArray;
@end
@implementation MyClass (private)
@synthesize someArray = _someArray;
// ^^^ error here: @synthesize not allowed in a category's implementation
@end
@implementation MyClass
...
@end
【问题讨论】:
-
为什么要有私有财产?什么时候可以直接在实例方法中访问
*_someArray? -
私有属性是放置 ivars 延迟加载逻辑等内容的好地方
标签: objective-c