iOS runtime forwardInvocation詳解及整理
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");
}
主調(diào)用類
TestModel *model = [[TestModel alloc] init]; [model testMethod];
TestModel本身沒有實(shí)現(xiàn)testMethod方法,但最終運(yùn)行后,程序沒有報(bào)錯(cuò),且TestModelHelper1和TestModelHelper2的testMethod方法都被執(zhí)行了
1.forwardingTargetForSelector同為消息轉(zhuǎn)發(fā),但在實(shí)踐層面上有什么區(qū)別?何時(shí)可以考慮把消息下放到forwardInvocation階段轉(zhuǎn)發(fā)?
forwardingTargetForSelector僅支持一個(gè)對象的返回,也就是說消息只能被轉(zhuǎn)發(fā)給一個(gè)對象
forwardInvocation可以將消息同時(shí)轉(zhuǎn)發(fā)給任意多個(gè)對象
2.methodSignatureForSelector如何實(shí)現(xiàn)?
methodSignatureForSelector用于描述被轉(zhuǎn)發(fā)的消息,描述的格式要遵循以下規(guī)則點(diǎn)擊打開鏈接

首先,先要了解的是,每個(gè)方法都有self和_cmd兩個(gè)默認(rèn)的隱藏參數(shù),self即接收消息的對象本身,_cmd即是selector選擇器,所以,描述的大概格式是:返回值@:參數(shù)。@即為self,:對應(yīng)_cmd(selector).返回值和參數(shù)根據(jù)不同函數(shù)定義做具體調(diào)整。
比如下面這個(gè)函數(shù)
-(void)testMethod;
返回值為void,沒有參數(shù),按照上面的表格中的符號說明,再結(jié)合上面提到的概念,這個(gè)函數(shù)的描述即為 v@:
v代表void,@代表self(self就是個(gè)對象,所以用@),:代表_cmd(selector)
再練一個(gè)
-(NSString *)testMethod2:(NSString *)str;
描述為 @@:@
第一個(gè)@代表返回值NSString*,對象;第二個(gè)@代表self;:代表_cmd(selector);第三個(gè)@代表參數(shù)str,NSString對象類型
如果實(shí)在拿不準(zhǔ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é)論一致
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖
本篇文章主要介紹了iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
iOS優(yōu)化UITableViewCell高度計(jì)算的一些事兒
這iOS開發(fā)中對于UITableViewCell高度自適應(yīng)的文章已經(jīng)很多很多,但都不是自己所需要的,下面篇文章主要給大家介紹了關(guān)于iOS優(yōu)化UITableViewCell高度計(jì)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-11-11
iOS定時(shí)器的選擇CADisplayLink NSTimer和GCD使用
這篇文章主要為大家介紹了iOS定時(shí)器的選擇CADisplayLink NSTimer和GCD使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
IOS開發(fā)中加載大量網(wǎng)絡(luò)圖片優(yōu)化方法
這篇文章主要介紹了IOS開發(fā)中加載大量網(wǎng)絡(luò)圖片如何優(yōu)化的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS開發(fā)之tableView實(shí)現(xiàn)左滑刪除功能
我們在使用一些應(yīng)用的時(shí)候,在滑動(dòng)一些聯(lián)系人的某一行的時(shí)候,會出現(xiàn)刪除、置頂、更多等等的按鈕,下面這篇文章主要就介紹了iOS用tableView實(shí)現(xiàn)左劃刪除功能的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2017-01-01
iOS中UIActivityIndicatorView的用法及齒輪等待動(dòng)畫實(shí)例
UIActivityIndicatorView活動(dòng)指示器最常見的用法便是用來制作那個(gè)程序中的齒輪轉(zhuǎn)動(dòng)的等待效果,接下來我們回來簡單整理iOS中UIActivityIndicatorView的用法及齒輪等待動(dòng)畫實(shí)例:2016-05-05

