在Objective-C中隐藏您的私有的位置

Phi*_*hil 5 cocoa objective-c private-members

什么是约定/规则/偏好/ 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
Run Code Online (Sandbox Code Playgroud)

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

Nik*_*uhe 4

如今,最佳实践是将它们放入@implementation或(非公开)类扩展中。

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

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