How to define private methods … sort of

There is no private or protected methods concept in Objective-C. I think it is related to the architecture of invoking method in Objective-C. Each method is invoked like [self method] style or [self performSelector:@selector(method)]. Internally, Objective-C runtime resolves method implementation by searching method name. This architecture gives flexibility to call method and also allows to have “category” but it is hard to support private method.

Apple still can enhance around this area to bring access control but until that time I think unnamed category is the simplest and easiest way to implement private method like declaration.

ClassA.h file

@interface ClassA {
}
- (void) publicMethod;
@end

ClassA.m file

@interface ClassA()
- (void) privateMethod;
@end
 
@implementation ClassA
- (void) publicMethod {
}
 
#pragma mark -
#pragma mark Private methods
- (void) privateMethod {
}
@end

Interesting discussions are going on at Stackoverflow.com

No comments yet.

Leave a Reply