我想在 MyClass.m 文件中定义私有(private)实例变量。在我看来,有两种方法可以做到这一点:
使用类扩展
@interface HelloViewController () { int value; }
在@implementation部分定义
@implementation HelloViewController { int value; }
哪个更好?
我认为最近 Apple 的编码风格是使用类扩展?
例如MasterViewController.m 由'Master-Detail Application Template'生成
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end
最佳答案
做到这一点的“现代 Objective-C”方法是在您的实现 block 中声明它们,如下所示:
@implementation ClassName {
int privateInteger;
MyObject *privateObject;
}
// method implementations etc...
@end
参见 this我之前的帖子有更多详细信息。
关于objective-c - 在 Objective-C 中定义私有(private)变量的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532862/