今天,講這個系列的第四篇,是關於Protocol
。數組
首先,咱們依然尋找最簡單的方法:bash
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(@"不存在該協議");
}
}
複製代碼
運行結果:ui
不存在該協議
複製代碼
奇了怪了,明明新建了,爲何找不到了,是否是由於沒有使用呢? 咱們隨便找個類去遵循這個協議,就拿咱們前幾篇新建的那個Person
類吧。 再運行一次:atom
name = PlayProtocol
複製代碼
此次沒錯了,最後的結論是,必須**register
**(只要有Class
遵循這個Protocol
,就算register
)的Protocol
才能經過objc_getProtocol
找到。 下面這個方法是判斷a
協議是否遵循b
協議:spa
BOOL
protocol_conformsToProtocol(Protocol * _Nullable proto,
Protocol * _Nullable other)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製代碼
咱們新建一個SingProtocol
協議,讓PlayProtocol
遵循SingProtocol
協議,即code
@protocol PlayProtocol <SingProtocol>
@end
複製代碼
咱們來實踐一下吧:orm
-(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?@"遵循":@"不遵循");
}
複製代碼
運行結果:server
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
的方法都測試完成了。