Category用法

今天给大家介绍一下我们常说的Category的详细用法,首先引用API文档的一段话:

You use categories to define additional methods of an existing class—even one whose source code is unavailable to you—without subclassing.

从这段话中我们可以看出Category(范畴)的本质:  
在不需要继承一个父类的情况下来实现另外的类中的某些方法,哪怕那个类的源代码不是开源的。
那么说到源代码不开源,我们首当其冲地就想到了iOS的SDK(也就是Cocoa Touch 框架),我们只能查看他们的头文件声明但是不能查看.m文件的实现。所以范畴用的比较多的情况就是在我们自己的类中去实现SDK中某个类的既有方法。
当然我们也可以在我们自己写的两个类中间使用category,这种情况一般有两种:
1.当我们一个类过于庞大的时候我们可以将它拆分成若干个category,在各个category的.m文件中去实现,这样避免主类的.m实现文件过长和混乱。
2.声明私有方法。

这些都应该好理解的,可能我的描述也有一些地方不够精确,但应该没有大的偏差,主要是用的时候理解对就行了。我们来看看category的声明方式:


<a target=_blank id="L1" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
           
           
#import "SystemClass.h" //这里的.h文可能是某些我们无法查看.m源文件的类的头文件声明,就是我刚刚说的第一种情况
@interface SystemClass (CategoryName) //这里的()括起来的地方就是我们Category(范畴)的名字了,在SDK源代码中或者XMPPFramework中我们也经常看到这种声明
// 在这里声明你要实现的方法
@end
 来自CODE的代码片
category1


然后是名字了,如果我们写好了一个范畴,比如叫做ObjecoderCategory,引用的Class叫做UITableView ,那么我们的Category的源文件名字就应该是UITableView+ObjecoderCategory.h .这在我们的XMPPFramework中的扩展类中,例如XMPPMessage+XEP0045,大家应该不陌生吧:

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
           
           
#import <Foundation/Foundation.h>
#import "XMPPMessage.h"
@interface XMPPMessage(XEP0045)
- (BOOL)isGroupChatMessage;
- (BOOL)isGroupChatMessageWithBody;
@end
 来自CODE的代码片
category2



如果你是想用Category声明私有方法,那么只需要在你的类的.h文件的@implementation标记前面加上这个Category的声明即可了,不需要单独去创建新的xxx+xxx.h文件,这个跟我们的@protocal写法很类似,例如:


<a target=_blank id="L1" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;"> 7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;"> 8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;"> 9</a>
           
           
#import "MyClass.h"
@interface MyClass (PrivateMethods)
// PrivateMethods范畴的私有方法声明
@end
@implementation MyClass
// 普通方法的声明
@end
 来自CODE的代码片
category3



除开这个,我们更为普通的情况是我们如果基于一个无法查看源代码的类之上写了一个Category,这个时候我们由于没有"父类"的.m文件的查看和修改权利,所以我们只有创建一个xxxx+xxxx.h/.m文件在里面实现Category的扩展方法,实现方式如下:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/mobanchengshuang/article/details/10631581#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
             
             
#import "SystemClass+CategoryName.h"
@implementationSystemClass ( CategoryName )
//category扩展的方法簇
@end
 来自CODE的代码片
category4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值