5

关于应在何处声明私有实例变量以及为什么要声明的约定/规则/首选项/diktat 是什么?

// someClass.h

@interface someClass : NSObject {
@private
// Here in the class declaration?
}
// ...
@end

// someClass.m

@interface someClass () {
// Here in the class extension? This seems to be obj-c's version
// of the C++ Pimpl idiom.
}
@end

@implementation someClass {
// Here in the class definition? BTW, what's the default scope here?
// Seems like it should be @private. I haven't been able to find much
// documentation about this declaration block.
}
// ...
@end

任何人都可以评论这三个部分的适当使用或指出一个好的网络资源吗?谢谢!

4

1 回答 1

4

今天,最好的做法是将它们放在@implementation或(非公共)类扩展中。

类的客户永远不会对 Ivar 感兴趣,因此它们不应该在公共 API(标头)中可见。

将它们放在@implementation 或类扩展中没有太大区别。为了保持一致,我总是将它们放在@implementation 中。

于 2013-03-04T22:46:21.383 回答