2

I believe a popular way to declare "private methods" in Objective-C is to create its class extension and declare methods that you would like to make as private.

I would like to know more in detail on how an class extension makes the methods work as private.

  • Update: I asked this question with the term empty category which is incorrect. I now changed it as class extension
Ryan Rho
  • 515
  • 5
  • 18
  • 3
    They’re not called empty categories: they’re called [class extensions](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html). Also, note that there’s no such thing as private methods in Objective-C — the runtime will happily invoke a class extension method if the corresponding message is sent in an arbitrary implementation file. –  Sep 17 '11 at 06:04
  • Right, so to make sure, I doubled quoted private methods. Thanks for the info. – Ryan Rho Sep 17 '11 at 20:56

2 Answers2

3

That's not an "empty category", it's a class extension. Read Bbum's explanation of them at the link I provided.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
2

That's because you create your empty category in your implementation file, not your header file so other classes can't access it.


//TestClass.h

@interface TestClass : NSObject 
{
}

-(void)publicMethod;

@end

//TestClass.m

@interface TestClass()

-(void)privateMethod;

@end

@implementation TestClass

-(void)publicMethod
{
NSLog (@"public");
}

-(void)privateMethod
{
NSLog (@"private");
}

@end


TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71