Where to hide your private individuals in Objective-C

What does the agreement / rule / preference / dictate mean 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 version
// of the C++ Pimpl idiom.
}
@end

@implementation someClass {
// Here in the class definition? BTW, what 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? Thank!

+5
source share
1 answer

Today, it’s best to place them in @implementationor in a (non-public) class extension.

Ivars are never of interest to class clients, so they should not be visible in a public API (header).

There is no big difference in putting them in @implementation or in a class extension. For consistency, I always put them in @implementation.

+4
source

All Articles