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 @end @implementation className (categoryName) //new method definitions @endExample 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; @end @implementation NSObject (display) -(void) privateDisplay { printf("This is private display\n"); } @end //This class is declared in the .h file //and inherited from NSObject @implementation categoryClass @end
No comments:
Post a Comment