What's the convention/rule/preference/diktat about where private instance variables should be declared and why?
// 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
Can anyone comment on the appropriate use of these three sections or point to a good web resource on this? Thanks!
Today it's best practice to put them in the @implementation
or in a (non-public) class extension.
Ivars are never of interest to clients of a class, so they should not be visible in the public API (the header).
There's not much difference in putting them in the @implementation or in a class-extension. For consistence I always put them in the @implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With