[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.
- [Objective-C] 使用Category给类添加private方法
- [Objective-C] 通过匿名category给category添加成员变量
- <拦截方法> Objective-C使用Category
- Objective-C使用Category
- Objective-C中的实例方法、类方法、Category、Protocol
- Category 类扩展,就是给原有类的添加方法。
- 101,使用Category给API里面的类添加新方法
- Objective-C语法之Category的使用
- Objective-C语法之Category的使用
- objective-c category类别的使用
- Objective-C语法之Category的使用
- Objective-C语法之Category的使用
- Objective-C语法之Category的使用
- Objective-C语法之Category的使用
- Objective-C语法之Category的使用
- 【iOS】Objective-C之Category的使用
- Objective-C语法之Category的使用
- Objective-C语法之Category的使用
- 2014年年终总结——获得集团优秀员工称号的心得体会
- 匿名管道 父子进程间通信
- 2种方法解决mysql主从不同步 .
- uil的简单常用操作
- MySQL保存jpg图片 测试通过
- [Objective-C] 使用Category给类添加private方法
- C# 中的扩展方法
- gdb调试常用命令
- python编程之bomb catcher 小游戏
- Android GPS学习笔记—LMS初始化
- Android消息推送
- 织梦列表和分页的调用
- 表哥布局
- 对于Fragment的一点认知