今天給大家介紹一下我們常說的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的代碼片
<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的代碼片
如果你是想用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的代碼片
除開這個,我們更爲普通的情況是我們如果基於一個無法查看源代碼的類之上寫了一個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
|