您的位置:首页 > 编程语言 > Go语言

Category用法

2015-07-18 00:27 585 查看
今天给大家介绍一下我们常说的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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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.csdn.net/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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: