[Objective-C] 使用Category给类添加private方法

来源:互联网 发布:macbook如何删除软件 编辑:程序博客网 时间:2024/05/17 22:44

转自:http://macdevelopertips.com/objective-c/private-methods.html


One common complaint for many new to Objective-C is that the language lacks support for private methods within a class. There are compiler directives for instance variables: @private, @protected (the default) and @public. However, if you want to hide methods, there is no specific directives to help. The example here builds on the previous post on working with categories.

Ultimately, there is no means to make a method private in Objective-C. However, one can hide methods in a class as I show here. It’s important to understand, this still does not make the methods private, it just makes them “harder to find” if you will.

To implement hidden methods (instance and/or class) you can use categories . Let’s begin with a file called SomeClass.h, the interface definition for SomeClass.

// ===========================// = File: SomeClass.h// = Interface for SomeClass// =========================== @interface SomeClass : Object -(void) msg;+(void) classMsg;  @end

Nothing too interesting here, simply an interface declaration for a class with two methods:msg andclassMsg . By definition (of the language) both methods are publicly accessible. Now, let’s look at the corresponding implementation file shown below. Begin at the bottom where I write the implementation for the two methods above. Again, nothing new, so far. Now, if you start at the top of this file, notice how I’ve added an interface forSomeClass (similar to the interface definition above), however, I added(hidden) to the definition, which now makes the interface definition a category. Just below the definition, I write the code for the two methods in the category.

// ===========================// = File: SomeClass.m// ===========================#import "SomeClass.h" // =================================// = Interface for hidden methods// =================================@interface SomeClass (hidden) +(void) hiddenClassMethod;-(void) hiddenInstanceMethod;  @end // =====================================// = Implementation of hidden methods// =====================================@implementation SomeClass (hidden) +(void) hiddenClassMethod{  printf( "Hidden class method.\n" );} -(void) hiddenInstanceMethod{  printf( "Hidden instance method\n" );} @end // ================================// = Implementation for SomeClass// ================================@implementation SomeClass -(void) msg{  printf("Inside msg()...\n");   [self hiddenInstanceMethod];  [SomeClass hiddenClassMethod];} +(void) classMsg{  printf("Inside classMsg()...\n");} @end

To see what this does for us, look at the code below, followed by the screenshot:

// ===========================// = File: Main.m// ===========================#import "SomeClass.h" int main (int argc, char *argv[]){  SomeClass *ptr = [[SomeClass alloc] init];   // Display message (including messages from hidden methods)  [ptr msg];   // Call a class method  [SomeClass classMsg];   // Compile warning (can't access hidden instance method)//  [ptr hiddenInstanceMethod];   // Compile warning (can't access hidden class method)//  [SomeClass hiddenClassMethod];     return 0;}

Notice how I can access the hidden instance and class methods from within the class. However, if I remove the comments from lines 17 and 20, to attempt access to the hidden methods, the compiler generates the following warnings:

I’m not sure if this is the only means to hide methods. If you know of another way (and have a code example), please post a comment.

The output of the application is shown here:

Download the code and give this a try within Xcode.


0 0
原创粉丝点击