Search code examples
objective-cclass-designprivate-methods

Objective-C call hidden methods


The following is an example of hidden method in Objective-C:

MyClass.m

#import "MyClass.h"

 
@interface MyClass (Private)
   -(void) privateMethod:(NSString *)arg1 and: (NSString*)arg2;
@end

@implementation MyClass

   -(void) publicMethod {
       NSLog(@"public method\n");
      /*call privateMethod with arg1, and arg2 ??? */
   }

   -(void) privateMethod:(NSString *)arg1 and: (NSString*)arg2{
       NSLog(@"Arg1 %@ and Arg2 %@", arg1, arg2);
   }

@end

I've read about private interface / methods declaration. But how to invoke them from an other public method ? I've tried [self privateMethod:@"Foo" and: @"Bar"] but it doesn't looks right.


Solution

  • Yes, [self privateMethod:@"Foo" and:@"Bar"] is correct. What looks wrong about it? And why didn't you just try it?

    (Btw, it's not really a private method, it's just hidden from the interface. Any outside object that knows the message signature can still call it. "Real" private methods don't exist in Objective-C.)