今天,講這個系列的第四篇,是關於Protocol
。
5.Protocol
首先,我們依然尋找最簡單的方法:
const char * _Nonnull
protocol_getName(Protocol * _Nonnull proto)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
這個方法是獲得協議的名稱。後面我們列印協議就通過這個函式。 既然可以通過協議來獲取名稱,那麼也可以通過名稱來獲取協議,就是下面這個方法:
Protocol * _Nullable
objc_getProtocol(const char * _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
我們新建一個協議類PlayProtocol
:
-(void)protocolCommen {
Protocol *protocol = objc_getProtocol("PlayProtocol");
if (protocol) {
const char* name = protocol_getName(protocol);
NSLog(@"name = %s",name);
}else {
NSLog(@"不存在該協議");
}
}
複製程式碼
執行結果:
不存在該協議
複製程式碼
奇了怪了,明明新建了,為什麼找不到了,是不是因為沒有使用呢?
我們隨便找個類去遵循這個協議,就拿我們前幾篇新建的那個Person
類吧。
再執行一次:
name = PlayProtocol
複製程式碼
這次沒錯了,最後的結論是,必須**register
**(只要有Class
遵循這個Protocol
,就算register
)的Protocol
才能通過objc_getProtocol
找到。
下面這個方法是判斷a
協議是否遵循b
協議:
BOOL
protocol_conformsToProtocol(Protocol * _Nullable proto,
Protocol * _Nullable other)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
我們新建一個SingProtocol
協議,讓PlayProtocol
遵循SingProtocol
協議,即
@protocol PlayProtocol <SingProtocol>
@end
複製程式碼
我們來實踐一下吧:
-(void)conformsToProtocol {
Protocol* playProtocol = objc_getProtocol("PlayProtocol");
Protocol* singProtocol = objc_getProtocol("SingProtocol");
BOOL isConform = protocol_conformsToProtocol(playProtocol, singProtocol);
NSLog(@"PlayProtocol協議 %@ SingProtocol協議",isConform?@"遵循":@"不遵循");
BOOL isConform2 = protocol_conformsToProtocol(singProtocol, playProtocol);
NSLog(@"SingProtocol協議 %@ PlayProtocol協議",isConform2?@"遵循":@"不遵循");
}
複製程式碼
執行結果:
2019-02-28 09:41:30.362087+0800 Runtime-Demo[86769:5791422] PlayProtocol協議 遵循 SingProtocol協議
2019-02-28 09:41:30.362134+0800 Runtime-Demo[86769:5791422] SingProtocol協議 不遵循 PlayProtocol協議
複製程式碼
我們可以PlayProtocol
協議遵循了SingProtocol
協議。
下面這個方法是指判斷2個協議是否相等。
BOOL
protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
我們新建一個RunProtocol
協議也遵循SingProtocol
。因為我們之前PlayProtocol
協議也遵循了SingProtocol
協議,並且這兩個協議都沒有各自的方法。那我們來測試下:
-(void)isEqual {
Protocol* playProtocol = objc_getProtocol("PlayProtocol");
Protocol* runProtocol = objc_getProtocol("SingProtocol");
BOOL isEqual = protocol_isEqual(playProtocol, runProtocol);
NSLog(@"%@",isEqual?@"相等":@"不相等");
}
複製程式碼
執行之後:
不相等
複製程式碼
又一次出乎意料,難道真的只有完全相等才行嗎?看下原始碼吧:
BOOL protocol_isEqual(Protocol *self, Protocol *other)
{
if (self == other) return YES;
if (!self || !other) return NO;
if (!protocol_conformsToProtocol(self, other)) return NO;
if (!protocol_conformsToProtocol(other, self)) return NO;
return YES;
}
複製程式碼
真的是隻有完全相等才能返回YES
:
那麼我們改下程式碼:
-(void)isEqual2 {
Protocol* playProtocol1 = objc_getProtocol("PlayProtocol");
NSLog(@"playProtocol1 = %@",playProtocol1);
Protocol* playProtocol2 = objc_getProtocol("PlayProtocol");
NSLog(@"playProtocol2 = %@",playProtocol2);
BOOL isEqual = protocol_isEqual(playProtocol1, playProtocol2);
NSLog(@"%@",isEqual?@"相等":@"不相等");
}
複製程式碼
執行結果:
2019-02-28 10:18:07.799200+0800 Runtime-Demo[87377:5813138] playProtocol1 = <Protocol: 0x10463e9a0>
2019-02-28 10:18:07.799250+0800 Runtime-Demo[87377:5813138] playProtocol2 = <Protocol: 0x10463e9a0>
2019-02-28 10:18:07.799301+0800 Runtime-Demo[87377:5813138] 相等
複製程式碼
只有當兩個協議物件完全相同(記憶體地址一樣),才能返回YES
。
下面兩個方法也是和Method
有關。
struct objc_method_description
protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel,
BOOL isRequiredMethod, BOOL isInstanceMethod)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
struct objc_method_description * _Nullable
protocol_copyMethodDescriptionList(Protocol * _Nonnull proto,
BOOL isRequiredMethod,
BOOL isInstanceMethod,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
protocol_getMethodDescription
找到某個協議的某個方法,protocol_copyMethodDescriptionList
找到某個協議的所有方法。我們在SingProtocol
協議裡面加幾個方法:
@protocol SingProtocol <NSObject>
@required
-(void)singFolkSongs;
+(void)singRockSongs;
@optional
-(void)singPopularSongs;
+(void)singMetalSongs;
@end
複製程式碼
我們先實踐下protocol_getMethodDescription
,protocol_getMethodDescription
有4個引數,proto
指定的協議,aSel
指定的方法,isRequiredMethod
是否是必要方法,isInstanceMethod
是否是例項方法:
-(void)getMethodDescription {
Protocol* singProtocol = objc_getProtocol("SingProtocol");
struct objc_method_description method = protocol_getMethodDescription(singProtocol, sel_registerName("singFolkSongs"), YES, YES);
NSLog(@"singFolkSongs方法 name = %s,types = %s", sel_getName(method.name) ,method.types);
struct objc_method_description method1 = protocol_getMethodDescription(singProtocol, sel_registerName("singRockSongs"), YES, NO);
NSLog(@"singRockSongs方法 name = %s,types = %s", sel_getName(method1.name) ,method1.types);
struct objc_method_description method2 = protocol_getMethodDescription(singProtocol, sel_registerName("singPopularSongs"), NO, YES);
NSLog(@"singPopularSongs方法 name = %s,types = %s", sel_getName(method2.name) ,method2.types);
struct objc_method_description method3 = protocol_getMethodDescription(singProtocol, sel_registerName("singMetalSongs"), NO, NO);
NSLog(@"singMetalSongs方法 name = %s,types = %s", sel_getName(method3.name) ,method3.types);
}
複製程式碼
執行結果:
2019-02-28 10:37:32.929473+0800 Runtime-Demo[87693:5820462] singFolkSongs方法 name = singFolkSongs,types = v16@0:8
2019-02-28 10:37:32.929514+0800 Runtime-Demo[87693:5820462] singRockSongs方法 name = singRockSongs,types = v16@0:8
2019-02-28 10:37:32.929528+0800 Runtime-Demo[87693:5820462] singPopularSongs方法 name = singPopularSongs,types = v16@0:8
2019-02-28 10:37:32.929540+0800 Runtime-Demo[87693:5820462] singMetalSongs方法 name = singMetalSongs,types = v16@0:8
複製程式碼
執行結果沒毛病!!!
我們再看下protocol_copyMethodDescriptionList
這個函式,是用來找某個協議符合條件的方法列表,引數含義和protocol_getMethodDescription
類似,那麼我就直接試煉了:
-(void)copyMethodDescriptionList {
Protocol* singProtocol = objc_getProtocol("SingProtocol");
unsigned int count1;
struct objc_method_description *methodList1 = protocol_copyMethodDescriptionList(singProtocol, YES, YES, &count1);
NSLog(@"-----必要方法以及例項方法-----");
for (unsigned int i = 0; i < count1; i++) {
struct objc_method_description method = methodList1[i];
NSLog(@"name = %s,types = %s", sel_getName(method.name) ,method.types);
}
free(methodList1);
unsigned int count2;
struct objc_method_description *methodList2 = protocol_copyMethodDescriptionList(singProtocol, YES, NO, &count2);
NSLog(@"-----必要方法以及類方法-----");
for (unsigned int i = 0; i < count2; i++) {
struct objc_method_description method = methodList2[i];
NSLog(@"name = %s,types = %s", sel_getName(method.name) ,method.types);
}
free(methodList2);
unsigned int count3;
struct objc_method_description *methodList3 = protocol_copyMethodDescriptionList(singProtocol, NO, NO, &count3);
NSLog(@"-----可選方法以及類方法-----");
for (unsigned int i = 0; i < count3; i++) {
struct objc_method_description method = methodList3[i];
NSLog(@"name = %s,types = %s", sel_getName(method.name) ,method.types);
}
free(methodList3);
unsigned int count4;
struct objc_method_description *methodList4 = protocol_copyMethodDescriptionList(singProtocol, NO, YES, &count4);
NSLog(@"-----可選方法以及例項方法-----");
for (unsigned int i = 0; i < count4; i++) {
struct objc_method_description method = methodList4[i];
NSLog(@"name = %s,types = %s", sel_getName(method.name) ,method.types);
}
free(methodList4);
}
複製程式碼
執行結果:
2019-02-28 10:46:29.075801+0800 Runtime-Demo[87839:5823598] -----必要方法以及例項方法-----
2019-02-28 10:46:29.075836+0800 Runtime-Demo[87839:5823598] name = singFolkSongs,types = v16@0:8
2019-02-28 10:46:29.075849+0800 Runtime-Demo[87839:5823598] -----必要方法以及類方法-----
2019-02-28 10:46:29.075857+0800 Runtime-Demo[87839:5823598] name = singRockSongs,types = v16@0:8
2019-02-28 10:46:29.075866+0800 Runtime-Demo[87839:5823598] -----可選方法以及類方法-----
2019-02-28 10:46:29.075873+0800 Runtime-Demo[87839:5823598] name = singMetalSongs,types = v16@0:8
2019-02-28 10:46:29.075882+0800 Runtime-Demo[87839:5823598] -----可選方法以及例項方法-----
2019-02-28 10:46:29.075889+0800 Runtime-Demo[87839:5823598] name = singPopularSongs,types = v16@0:8
複製程式碼
和我們分析的一樣。 既然可以從協議裡獲得方法,那麼也可以獲得協議裡的屬性:
objc_property_t _Nullable
protocol_getProperty(Protocol * _Nonnull proto,
const char * _Nonnull name,
BOOL isRequiredProperty, BOOL isInstanceProperty)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
objc_property_t _Nonnull * _Nullable
protocol_copyPropertyList(Protocol * _Nonnull proto,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
objc_property_t _Nonnull * _Nullable
protocol_copyPropertyList2(Protocol * _Nonnull proto,
unsigned int * _Nullable outCount,
BOOL isRequiredProperty, BOOL isInstanceProperty)
OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
複製程式碼
protocol_getProperty
是獲取協議裡的屬性,protocol_copyPropertyList
和protocol_copyPropertyList2
都是獲取協議裡的屬性列表。protocol_copyPropertyList2
是更加細分條件的去獲取協議裡的屬性列表。
我們在SingProtocol
裡新增若干屬性,如下:
@protocol SingProtocol <NSObject>
@required
-(void)singFolkSongs;
+(void)singRockSongs;
@property(nonatomic,copy)NSString* folkSongs;
@property(nonatomic,copy,class)NSString* rockSongs;
@optional
-(void)singPopularSongs;
+(void)singMetalSongs;
@property(nonatomic,copy)NSString* popularSongs;
@property(nonatomic,copy,class)NSString* metalSongs;
@end
複製程式碼
在上面我們看到了陌生的class
關鍵詞,iOS8
之後,LLVM
已經支援顯式宣告類屬性了,這是為了與Swift
中的類屬性互操作而引入的,大家可以找下相關文件。
另外在協議的屬性裡面,是不分@optional
和@required
,我們在原始碼裡可以看到:
static property_t *
protocol_getProperty_nolock(protocol_t *proto, const char *name,
bool isRequiredProperty, bool isInstanceProperty)
{
runtimeLock.assertLocked();
if (!isRequiredProperty) {
// Only required properties are currently supported.
return nil;
}
property_list_t *plist = isInstanceProperty ?
proto->instanceProperties : proto->classProperties();
if (plist) {
for (auto& prop : *plist) {
if (0 == strcmp(name, prop.name)) {
return ∝
}
}
}
if (proto->protocols) {
uintptr_t i;
for (i = 0; i < proto->protocols->count; i++) {
protocol_t *p = remapProtocol(proto->protocols->list[i]);
property_t *prop =
protocol_getProperty_nolock(p, name,
isRequiredProperty,
isInstanceProperty);
if (prop) return prop;
}
}
return nil;
}
複製程式碼
protocol_getProperty
的底層就是呼叫的這個方法,我們可以看到裡面:
if (!isRequiredProperty) {
// Only required properties are currently supported.
return nil;
}
複製程式碼
這裡明確說明了目前只支援required
的屬性,一旦傳入isRequiredProperty
為NO
的話,直接返回nil
。
知道了這些概念,我們實踐下:
-(void)getProperty_Protocol {
Protocol* singProtocol = objc_getProtocol("SingProtocol");
objc_property_t rockSongsProperty = protocol_getProperty(singProtocol, "rockSongs", YES, NO);
const char* rockSongsName = property_getName(rockSongsProperty);
NSLog(@"rockSongs 屬性 name = %s",rockSongsName);
objc_property_t folkSongsProperty = protocol_getProperty(singProtocol, "folkSongs", YES, YES);
const char* folkSongsName = property_getName(folkSongsProperty);
NSLog(@"folkSongs 屬性 name = %s",folkSongsName);
objc_property_t popularSongsProperty = protocol_getProperty(singProtocol, "popularSongs", YES, YES);
const char* popularSongsName = property_getName(popularSongsProperty);
NSLog(@"popularSongs 屬性 name = %s",popularSongsName);
objc_property_t metalSongsProperty = protocol_getProperty(singProtocol, "metalSongs", YES, NO);
const char* metalSongsName = property_getName(metalSongsProperty);
NSLog(@"metalSongs 屬性 name = %s",metalSongsName);
}
複製程式碼
列印結果:
2019-02-28 11:49:17.619145+0800 Runtime-Demo[88910:5849614] rockSongs 屬性 name = rockSongs
2019-02-28 11:49:17.619184+0800 Runtime-Demo[88910:5849614] folkSongs 屬性 name = folkSongs
2019-02-28 11:49:17.619196+0800 Runtime-Demo[88910:5849614] popularSongs 屬性 name = popularSongs
2019-02-28 11:49:17.619207+0800 Runtime-Demo[88910:5849614] metalSongs 屬性 name = metalSongs
複製程式碼
沒毛病!!!
同樣,我們實踐下protocol_copyPropertyList
和protocol_copyPropertyList2
方法:
-(void)copyPropertyList_protocol {
Protocol* singProtocol = objc_getProtocol("SingProtocol");
NSLog(@"-----copyPropertyList------");
unsigned int count;
objc_property_t* propertyList = protocol_copyPropertyList(singProtocol, &count);
for (unsigned int i = 0; i < count; i++) {
objc_property_t property = propertyList[i];
NSLog(@"name = %s",property_getName(property));
}
free(propertyList);
NSLog(@"-----copyPropertyList2---instance propertys---");
unsigned int count2;
objc_property_t* propertyList2 = protocol_copyPropertyList2(singProtocol, &count2, YES, YES);
for (unsigned int i = 0; i < count2; i++) {
objc_property_t property = propertyList2[i];
NSLog(@"name = %s",property_getName(property));
}
free(propertyList2);
NSLog(@"-----copyPropertyList2---class propertys---");
unsigned int count3;
objc_property_t* propertyList3 = protocol_copyPropertyList2(singProtocol, &count3, YES, NO);
for (unsigned int i = 0; i < count3; i++) {
objc_property_t property = propertyList3[i];
NSLog(@"name = %s",property_getName(property));
}
free(propertyList3);
}
複製程式碼
執行結果:
2019-02-28 13:30:34.259328+0800 Runtime-Demo[90302:5888309] -----copyPropertyList------
2019-02-28 13:30:34.259367+0800 Runtime-Demo[90302:5888309] name = folkSongs
2019-02-28 13:30:34.259378+0800 Runtime-Demo[90302:5888309] name = popularSongs
2019-02-28 13:30:34.259387+0800 Runtime-Demo[90302:5888309] -----copyPropertyList2---instance propertys---
2019-02-28 13:30:34.259395+0800 Runtime-Demo[90302:5888309] name = folkSongs
2019-02-28 13:30:34.259403+0800 Runtime-Demo[90302:5888309] name = popularSongs
2019-02-28 13:30:34.259411+0800 Runtime-Demo[90302:5888309] -----copyPropertyList2---class propertys---
2019-02-28 13:30:34.259418+0800 Runtime-Demo[90302:5888309] name = rockSongs
2019-02-28 13:30:34.259426+0800 Runtime-Demo[90302:5888309] name = metalSongs
複製程式碼
根據列印結果protocol_copyPropertyList
方法只能獲得例項屬性的屬性列表。protocol_copyPropertyList2
方法根據你傳的isInstanceProperty
的不同而返回不同的屬性列表(例項屬性列表或者類屬性列表)。
那麼,如何檢視一個類是否遵循某個協議呢,當然是下面這個方法:
BOOL
class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
我們已知Person
類已經遵循了PlayProtocol
,那麼測試下:
-(void)conformsToProtocol_class {
Protocol* playProtocol = objc_getProtocol("PlayProtocol");
Protocol* runProtocol = objc_getProtocol("RunProtocol");
BOOL isConforms = class_conformsToProtocol(objc_getClass("Person"), playProtocol);
BOOL isConforms2 = class_conformsToProtocol(objc_getClass("Person"), runProtocol);
NSLog(@"Person %@ PlayProtocol協議",isConforms?@"遵循":@"不遵循");
NSLog(@"Person %@ RunProtocol協議",isConforms2?@"遵循":@"不遵循");
}
複製程式碼
執行結果:
2019-02-28 13:57:46.292107+0800 Runtime-Demo[90742:5897636] Person 遵循 PlayProtocol協議
2019-02-28 13:57:46.292143+0800 Runtime-Demo[90742:5897636] Person 不遵循 RunProtocol協議
複製程式碼
完全正確!!!
Protocol * __unsafe_unretained _Nonnull * _Nullable
protocol_copyProtocolList(Protocol * _Nonnull proto,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
這個方法是獲得一個協議所遵循的協議的列表。新建一個新協議EatProtocol
,遵循RunProtocol
,SingProtocol
,PlayProtocol
,並讓Person
類遵循EatProtocol
(目的是能獲得這個協議)如下:
@protocol EatProtocol <RunProtocol,SingProtocol,PlayProtocol>
@end
複製程式碼
準備工作好了,那麼久開始了:
-(void)copyProtocolList {
Protocol* eatProtocol = objc_getProtocol("EatProtocol");
unsigned int count;
__unsafe_unretained Protocol** protocolList = protocol_copyProtocolList(eatProtocol, &count);
for (unsigned int i = 0; i < count ; i++) {
Protocol* protocol = protocolList[i];
NSLog(@"%s",protocol_getName(protocol));
}
free(protocolList);
}
複製程式碼
執行結果:
2019-02-28 14:17:35.718944+0800 Runtime-Demo[91079:5905870] RunProtocol
2019-02-28 14:17:35.718983+0800 Runtime-Demo[91079:5905870] SingProtocol
2019-02-28 14:17:35.718992+0800 Runtime-Demo[91079:5905870] PlayProtocol
複製程式碼
事實上也遵循了這三個協議。 下面這個方法是獲得某個類所遵循的協議陣列
Protocol * __unsafe_unretained _Nonnull * _Nullable
class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
我們在Person
類裡遵循了PlayProtocol
,EatProtocol
兩個協議:
-(void)copyProtocolList_class {
Class class = objc_getClass("Person");
unsigned int count;
__unsafe_unretained Protocol** protocolList = class_copyProtocolList(class, &count);
for (unsigned int i = 0; i < count ; i++) {
Protocol* protocol = protocolList[i];
NSLog(@"%s",protocol_getName(protocol));
}
free(protocolList);
}
複製程式碼
執行結果:
2019-02-28 14:26:38.842578+0800 Runtime-Demo[91226:5908955] PlayProtocol
2019-02-28 14:26:38.842616+0800 Runtime-Demo[91226:5908955] EatProtocol
複製程式碼
Person
類事實遵循了PlayProtocol
,EatProtocol
兩個協議。
下面這個函式是獲得整個工程裡面所遵循的協議:
Protocol * __unsafe_unretained _Nonnull * _Nullable
objc_copyProtocolList(unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
執行結果:
2019-02-28 14:30:14.378254+0800 Runtime-Demo[91287:5910354] SCNAnimation
2019-02-28 14:30:14.378305+0800 Runtime-Demo[91287:5910354] GEOBatchOpportunisticTileDownloaderDelegate
2019-02-28 14:30:14.378321+0800 Runtime-Demo[91287:5910354] PTSettingsKeyObserver
2019-02-28 14:30:14.378339+0800 Runtime-Demo[91287:5910354] _SFPBUserReportRequest
2019-02-28 14:30:14.378354+0800 Runtime-Demo[91287:5910354] _UIAlertControllerTextFieldViewControllerContaining
2019-02-28 14:30:14.378370+0800 Runtime-Demo[91287:5910354] SFVerticalLayoutCardSection
2019-02-28 14:30:14.378386+0800 Runtime-Demo[91287:5910354] MKInfoCardThemeListener
......
......
複製程式碼
我們會發現,列印了很多協議,其中大多數都是系統自帶的協議,也有我們自定義的協議。
和Class一樣還能動態的生成Protocol
。
Protocol * _Nullable
objc_allocateProtocol(const char * _Nonnull name)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
void
objc_registerProtocol(Protocol * _Nonnull proto)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
這兩個函式和objc_allocateClassPair
和objc_registerClassPair
一樣,必須register才能使用。
-(void)allocateProtocol {
Protocol* protocol = objc_allocateProtocol("TestProtocol");
objc_registerProtocol(protocol);
NSLog(@"%s",protocol_getName(protocol));
}
複製程式碼
執行結果:
TestProtocol
複製程式碼
下面幾個方法是給協議新增內容的。
void
protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name,
const char * _Nullable types,
BOOL isRequiredMethod, BOOL isInstanceMethod)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
這個方法是給協議新增Method
的。給協議新增方法有兩種情況,第一,是給已經建立好並且已經register
的協議新增方法,第二,是給動態生成的協議新增方法。
第一種情況:
-(void)addMethodDescription1 {
Protocol* eatProtocol = objc_getProtocol("EatProtocol");
SEL selector = sel_registerName("test");
const char* type = "v@:";
BOOL isRequiredMethod = YES;
BOOL isInstanceMethod = YES;
protocol_addMethodDescription(eatProtocol, selector, type, isRequiredMethod, isInstanceMethod);
struct objc_method_description method = protocol_getMethodDescription(eatProtocol, sel_registerName("test"), YES, YES);
NSLog(@"test方法 name = %s,types = %s", sel_getName(method.name) ,method.types);
}
複製程式碼
執行結果:
objc[2121]: protocol_addMethodDescription: protocol 'EatProtocol' is not under construction!
2019-03-01 09:30:56.232714+0800 Runtime-Demo[2121:6266526] test方法 name = <null selector>,types = (null)
複製程式碼
列印結果說EatProtocol
不在建設中,新增方法也失敗!
第二種情況:
-(void)addMethodDescription2 {
Protocol* protocol = objc_allocateProtocol("TestProtocol");
SEL selector = sel_registerName("test");
const char* type = "v@:";
BOOL isRequiredMethod = YES;
BOOL isInstanceMethod = YES;
protocol_addMethodDescription(protocol, selector, type, isRequiredMethod, isInstanceMethod);
objc_registerProtocol(protocol);
struct objc_method_description method = protocol_getMethodDescription(protocol, sel_registerName("test"), YES, YES);
NSLog(@"test方法 name = %s,types = %s", sel_getName(method.name) ,method.types);
}
複製程式碼
執行結果:
2019-03-01 09:32:34.581517+0800 Runtime-Demo[2148:6267322] test方法 name = test,types = v@:
複製程式碼
下面這個方法是新增屬性:
void
protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name,
const objc_property_attribute_t * _Nullable attributes,
unsigned int attributeCount,
BOOL isRequiredProperty, BOOL isInstanceProperty)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
這個函式和protocol_addMethodDescription
一樣,只有通過objc_allocateProtocol
生成的協議才能新增屬性。
-(void)addProperty_protocol {
Protocol* protocol = objc_allocateProtocol("TestProtocol");
const char* name = "juice";
unsigned int count = 5;
objc_property_attribute_t attributeList[count];
objc_property_attribute_t attribute1 ;
attribute1.name = "T";
attribute1.value = "NSString";
objc_property_attribute_t attribute2 ;
attribute2.name = "V";
attribute2.value = "_juice";
objc_property_attribute_t attribute3 ;
attribute3.name = "N";
attribute3.value = "";
objc_property_attribute_t attribute4 ;
attribute4.name = "C";
attribute4.value = "";
objc_property_attribute_t attribute5 ;
attribute5.name = "R";
attribute5.value = "";
attributeList[0] = attribute1;
attributeList[1] = attribute2;
attributeList[2] = attribute3;
attributeList[3] = attribute4;
attributeList[4] = attribute5;
BOOL isRequiredProperty = YES;
BOOL isInstanceProperty = YES;
protocol_addProperty(protocol, name, (const objc_property_attribute_t*)attributeList, count, isRequiredProperty, isInstanceProperty);
objc_registerProtocol(protocol);
objc_property_t property = protocol_getProperty(protocol, "juice", YES, YES);
NSLog(@"name = %s",property_getName(property));
}
複製程式碼
執行結果:
name = juice
複製程式碼
下面的函式是指的是讓proto
協議遵循addition
協議。
void
protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
proto
協議必須是仍在構建當中(alloc
但是沒有register
)而addition
是要已經構建好的。我們讓TestProtocol
遵循SingProtocol
協議。
-(void)addProtocol_protocol {
Protocol* protocol = objc_allocateProtocol("TestProtocol");
Protocol* protocol2 = objc_getProtocol("SingProtocol");
protocol_addProtocol(protocol, protocol2);
objc_registerProtocol(protocol);
BOOL isConform = protocol_conformsToProtocol(protocol, protocol2);
NSLog(@"isConform = %d",isConform);
}
複製程式碼
執行結果:
isConform = 1
複製程式碼
下面這個方法是讓一個類遵循protocol
協議
BOOL
class_addProtocol(Class _Nullable cls, Protocol * _Nonnull protocol)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
目前Cat
類沒有遵循任何協議,這裡我打算讓Cat
遵循PlayProtocol
協議:
-(void)addProtocol_class {
Protocol* protocol = objc_getProtocol("PlayProtocol");
Class class = objc_getClass("Cat");
class_addProtocol(class, protocol);
BOOL isConform = class_conformsToProtocol(class, protocol);
NSLog(@"isConform = %d",isConform);
}
複製程式碼
執行結果:
isConform = 1
複製程式碼
這樣,所有關於Protocol
的方法都測試完成了。