新聞中心
小編給大家分享一下iOS中runtime forwardInvocation的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
專注于為中小企業(yè)提供成都網(wǎng)站制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)敘州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
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 *h2 = [[TestModelHelper1 alloc] init]; TestModelHelper2 *h3 = [[TestModelHelper2 alloc] init]; [anInvocation invokeWithTarget:h2]; [anInvocation invokeWithTarget:h3]; } }
TestModelHelper1
-(void)testMethod { NSLog(@"i am TestModelHelper1"); } TestModelHelper2 [objc] view plain copy -(void)testMethod { NSLog(@"i am TestModelHelper2"); }
主調(diào)用類
TestModel *model = [[TestModel alloc] init]; [model testMethod];
TestModel本身沒(méi)有實(shí)現(xiàn)testMethod方法,但最終運(yùn)行后,程序沒(méi)有報(bào)錯(cuò),且TestModelHelper1和TestModelHelper2的testMethod方法都被執(zhí)行了
1.forwardingTargetForSelector同為消息轉(zhuǎn)發(fā),但在實(shí)踐層面上有什么區(qū)別?何時(shí)可以考慮把消息下放到forwardInvocation階段轉(zhuǎn)發(fā)?
forwardingTargetForSelector僅支持一個(gè)對(duì)象的返回,也就是說(shuō)消息只能被轉(zhuǎn)發(fā)給一個(gè)對(duì)象
forwardInvocation可以將消息同時(shí)轉(zhuǎn)發(fā)給任意多個(gè)對(duì)象
2.methodSignatureForSelector如何實(shí)現(xiàn)?
methodSignatureForSelector用于描述被轉(zhuǎn)發(fā)的消息,描述的格式要遵循以下規(guī)則點(diǎn)擊打開(kāi)鏈接
首先,先要了解的是,每個(gè)方法都有self和_cmd兩個(gè)默認(rèn)的隱藏參數(shù),self即接收消息的對(duì)象本身,_cmd即是selector選擇器,所以,描述的大概格式是:返回值@:參數(shù)。@即為self,:對(duì)應(yīng)_cmd(selector).返回值和參數(shù)根據(jù)不同函數(shù)定義做具體調(diào)整。
比如下面這個(gè)函數(shù)
-(void)testMethod;
返回值為void,沒(méi)有參數(shù),按照上面的表格中的符號(hào)說(shuō)明,再結(jié)合上面提到的概念,這個(gè)函數(shù)的描述即為 v@:
v代表void,@代表self(self就是個(gè)對(duì)象,所以用@),:代表_cmd(selector)
再練一個(gè)
-(NSString *)testMethod2:(NSString *)str;
描述為 @@:@
第一個(gè)@代表返回值NSString*,對(duì)象;第二個(gè)@代表self;:代表_cmd(selector);第三個(gè)@代表參數(shù)str,NSString對(duì)象類型
如果實(shí)在拿不準(zhǔn),不會(huì)寫,還可以簡(jiǎn)單寫段代碼,借助method_getTypeEncoding方法去查看某個(gè)函數(shù)的描述,比如
-(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); }
把數(shù)字去掉,剩下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 @""; }
結(jié)果是@@:@,與之前結(jié)論一致
以上是“iOS中runtime forwardInvocation的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:iOS中runtimeforwardInvocation的示例分析
標(biāo)題鏈接:http://fisionsoft.com.cn/article/psipgo.html