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的声明方式:
来自CODE的代码片
category1
然后是名字了,如果我们写好了一个范畴,比如叫做ObjecoderCategory,引用的Class叫做UITableView ,那么我们的Category的源文件名字就应该是UITableView+ObjecoderCategory.h .这在我们的XMPPFramework中的扩展类中,例如XMPPMessage+XEP0045,大家应该不陌生吧:
来自CODE的代码片
category2
如果你是想用Category声明私有方法,那么只需要在你的类的.h文件的@implementation标记前面加上这个Category的声明即可了,不需要单独去创建新的xxx+xxx.h文件,这个跟我们的@protocal写法很类似,例如:
来自CODE的代码片
category3
除开这个,我们更为普通的情况是我们如果基于一个无法查看源代码的类之上写了一个Category,这个时候我们由于没有"父类"的.m文件的查看和修改权利,所以我们只有创建一个xxxx+xxxx.h/.m文件在里面实现Category的扩展方法,实现方式如下:
来自CODE的代码片
category4
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> | #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
相关文章推荐
- 详解Django中的form库的使用
- 在Django的视图中使用form对象的方法
- 在Django的URLconf中进行函数导入的方法
- 在Django的URLconf中使用多个视图前缀的方法
- 在Django的URLconf中使用命名组的方法
- Django中传递参数到URLconf的视图函数中的方法
- 在Django框架中伪造捕捉到的URLconf值的方法
- Django的URLconf中使用缺省视图参数的方法
- OC基础---protocol、category和继承的区别
- Django笔记 —— 表单(form)
- 安装go语言环境
- 在Django框架中编写Contact表单的教程
- Algorithms—142.Linked List Cycle II
- 简单解析Django框架中的表单验证
- 改进Django中的表单的简单方法
- [渗透测试] Google Hack
- Golang通过Thrift框架完美实现跨语言调用
- Django数据API
- Golang的Interface是个什么鬼
- UVALive 6886 (LA 6886) Golf Bot FFT