Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top