我有以下类接口(interface):
@interface MyClass : NSObject
@property int publicProperty;
@end
然后是实现:
@interface MyClass() // class extension
- (void)privateMethod; // private methods
@end
@implementation MyClass {
int _privateProperty;
}
@property int privateProperty = _privateProperty;
@end
这是 Apple 人在 WWDC 上展示的内容,但是有什么理由不将 _privateProperty 放在类扩展中,例如:
@interface MyClass() // class extension
{
int _privateProperty;
}
- (void)privateMethod; // private methods
@end
谢谢!
最佳答案
我通常在实现中通过扩展“强制”私有(private)
在你的标题中
@interface MyClass : NSObject
{
}
@property (nonatomic, assign) int publicProperty;
@end
在你的实现文件中:
@interface MyClass ()
@property (nonatomic, assign) int privateProperty;
@end
@implementation MyClass
@synthesize privateProperty;
@synthesize publicProperty;
@end
关于objective-c - Objective-C : where to declare private instance properties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11304327/