国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS runtime forwardInvocation詳解及整理

2020-07-26 02:56:41
字體:
來源:轉載
供稿:網友

 iOS runtime forwardInvocation詳解

代碼:

TestModel

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {   if(aSelector == @selector(testMethod))   {     return [NSMethodSignature signatureWithObjCTypes:"v@:"];   }   return nil; }   -(void)forwardInvocation:(NSInvocation *)anInvocation {   if (anInvocation.selector == @selector(testMethod))   {     TestModelHelper1 *h1 = [[TestModelHelper1 alloc] init];     TestModelHelper2 *h2 = [[TestModelHelper2 alloc] init];     [anInvocation invokeWithTarget:h1];     [anInvocation invokeWithTarget:h2];   } } 

TestModelHelper1

-(void)testMethod {   NSLog(@"i am TestModelHelper1"); } TestModelHelper2[objc] view plain copy-(void)testMethod {   NSLog(@"i am TestModelHelper2"); } 

主調用類

TestModel *model = [[TestModel alloc] init]; [model testMethod]; 

TestModel本身沒有實現testMethod方法,但最終運行后,程序沒有報錯,且TestModelHelper1和TestModelHelper2的testMethod方法都被執行了

1.forwardingTargetForSelector同為消息轉發,但在實踐層面上有什么區別?何時可以考慮把消息下放到forwardInvocation階段轉發?

forwardingTargetForSelector僅支持一個對象的返回,也就是說消息只能被轉發給一個對象

forwardInvocation可以將消息同時轉發給任意多個對象

2.methodSignatureForSelector如何實現?

methodSignatureForSelector用于描述被轉發的消息,描述的格式要遵循以下規則點擊打開鏈接

首先,先要了解的是,每個方法都有self和_cmd兩個默認的隱藏參數,self即接收消息的對象本身,_cmd即是selector選擇器,所以,描述的大概格式是:返回值@:參數。@即為self,:對應_cmd(selector).返回值和參數根據不同函數定義做具體調整。

比如下面這個函數

-(void)testMethod; 

返回值為void,沒有參數,按照上面的表格中的符號說明,再結合上面提到的概念,這個函數的描述即為   v@:

v代表void,@代表self(self就是個對象,所以用@),:代表_cmd(selector)

再練一個

-(NSString *)testMethod2:(NSString *)str;

描述為 @@:@

第一個@代表返回值NSString*,對象;第二個@代表self;:代表_cmd(selector);第三個@代表參數str,NSString對象類型

如果實在拿不準,不會寫,還可以簡單寫段代碼,借助method_getTypeEncoding方法去查看某個函數的描述,比如

-(void)testMethod {   NSLog(@"i am TestModelHelper1");   Method method = class_getInstanceMethod(self.class, @selector(testMethod));   const char *des = method_getTypeEncoding(method);   NSString *desStr = [NSString stringWithCString:des encoding:NSUTF8StringEncoding];   NSLog(@"%@",desStr); } 


把數字去掉,剩下v@:,與之前我們的描述一致

-(NSString *)testMethod2:(NSString *)str {   Method method = class_getInstanceMethod(self.class, @selector(testMethod2:));   const charchar *des = method_getTypeEncoding(method);   NSString *desStr = [NSString stringWithCString:des encoding:NSUTF8StringEncoding];   NSLog(@"%@",desStr);   return @""; } 

結果是@@:@,與之前結論一致

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐亭县| 庄浪县| 磴口县| 建宁县| 永宁县| 罗山县| 扎兰屯市| 房产| 绿春县| 晋城| 孙吴县| 久治县| 六枝特区| 芜湖市| 汽车| 大同市| 太保市| 大埔县| 西乌珠穆沁旗| 奇台县| 塔城市| 澄迈县| 伊吾县| 温州市| 井冈山市| 德令哈市| 苍溪县| 禄丰县| 深圳市| 富阳市| 伊川县| 七台河市| 阳江市| 即墨市| 保康县| 阿克| 龙井市| 邳州市| 鲁山县| 金寨县| 泌阳县|