Friday, November 23, 2012

private method objective C

In objectiveC, privilege levels are allowed for  only data variables and not for methods. So public, private and protect  access specifiers are valid for only data variables. But there is a feature called Category which is used for declaring private methods in objectiveC.

Using category we can implement private methods. But we need to declare the methods in .m file and not in .h file. So declaration and definition will be in the same source file.

Syntax for the private method:
//need to add both in .m file
@interface className (categoryName)
//new methods

@implementation className (categoryName)
//new method definitions

Example for private method: In this example, we add one private method to the NSObject class using category and its name is display which is enclosed in the braces. 
@interface NSObject (display)
-(void) privateDisplay;

@implementation NSObject (display)
-(void) privateDisplay
    printf("This is private display\n");

//This class is declared in the .h file 
//and inherited from NSObject
@implementation categoryClass

No comments:

Popular Posts