0

Possible Duplicate:
Best way to define private methods for a class in Objective-C

Hi, Can i have private methods for any class in a cocoa application? if yse, how?

Community
  • 1
  • 1
Sahitya Tarumani
  • 419
  • 5
  • 14

2 Answers2

2

Yes you can!

in your *.m file (Implementation)

#import "MyClass.h"

@interface MyClass() 
- (void)privateMethod();
@end 

@implementation MyClass

- (void)dealloc {
    [super dealloc];
}

- (void)privateMethod
{
   NSlog(@"myPrivateMethod");
}
@end
Konrad77
  • 2,515
  • 1
  • 19
  • 36
  • 3
    This is the best you can do in Obj-C, but note that it is not truly private -- another class can still invoke the private method, (albeit with a compile-time warning `'MyClass' may not respond to '-privateMethod'`). See the detailed answers to the linked questions. – David Gelhar Feb 23 '11 at 14:25
0

If program Java, for example, I'd say to you that private methods are not simple to implement.

To implement them, you should implement, in the .m file, interfaces containing the signature of those methods you want to be private. Since imports "always" regards to .h files, this solution serves as private method implementation solution.

Resuming: use .h to expose your public methods. Use interfaces inside the .m to enable methods to be used as private.

Leandro
  • 1,076
  • 12
  • 21