今天给大家介绍一下我们常说的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> |
来自CODE的代码片
category1
<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> |
来自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> |
来自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> |