Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to Hide Your Privates in Objective-C [duplicate]

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!

like image 301
Phil Avatar asked Oct 05 '22 20:10

Phil


1 Answers

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.

like image 198
Nikolai Ruhe Avatar answered Oct 12 '22 08:10

Nikolai Ruhe