亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Objective-C中一種消息處理方法performSelector

系統(tǒng) 2980 0

?

Objective-C中調(diào)用函數(shù)的方法是“消息傳遞”,這個(gè)和普通的函數(shù)調(diào)用的區(qū)別是,你可以隨時(shí)對(duì)一個(gè)對(duì)象傳遞任何消息,而不需要在編譯的時(shí)候聲明這些方法。所以O(shè)bjective-C可以在runtime的時(shí)候傳遞人和消息。

?

首先介紹兩個(gè)方法 SEL和@selector

根據(jù)Apple Objective-C Runtime Reference 官方文檔這個(gè)傳遞消息的函數(shù)就是? id ?objc_msgSend( id? theReceiver,? SEL theSelector, …)

theReceiver是接受消息的對(duì)象類型是id,theSelector是消息名稱類型是SEL。下邊代碼我們來看看如何來生成一個(gè)SEL,如果傳遞消息。

首先建立一個(gè)簡(jiǎn)單的函數(shù)

- ( void ) fooNoInputs {

NSLog( @"Does nothing" );

}

然后調(diào)用它

[ self ?performSelector: @selector (fooNoInputs)];

第二個(gè)試驗(yàn)看看如何在消息中傳遞參數(shù)

我們建立一個(gè)有input參數(shù)的函數(shù)

- ( void ) fooOneIput:(NSString*) first {

NSLog( @"Logs %@" , first);

}

然后調(diào)用它

[ self ?performSelector: @selector (fooOneInput:) withObject: @"first" ];

第三個(gè)試驗(yàn)更多的參數(shù)

- ( void ) fooFirstInput:(NSString*) first secondInput:(NSString*) second {

NSLog( @"Logs %@ then %@" , first, second);

}

然后調(diào)用它

[ self ?performSelector: @selector (fooFirstInput:secondInput:) withObject: @"first" withObject: @"second" ];

第四個(gè)試驗(yàn)如何建立動(dòng)態(tài)的函數(shù),然后調(diào)用他們?我們需要建立一個(gè)selector

SEL ?myTestSelector =? @selector (myTest:);

并且我們調(diào)用的函數(shù)在另外一個(gè)Class內(nèi)

- ( void )abcWithAAA: ( NSNumber ?*)number {

int ?primaryKey = [number? intValue ];

NSLog ( "%i" , primaryKey);

}

MethodForSelectors ?* mfs = [[ MethodForSelectors ? alloc ]init];

NSArray ?*Arrays = [ NSArray ? arrayWithObjects : @"AAA" ,? @"BBB" ,? nil ];

for ?(? NSString ?*array? in ?Arrays ){

SEL ?customSelector =? NSSelectorFromString ([ NSString stringWithFormat : @"abcWith%@:" , array]);

mfs = [[ MethodForSelectors ? alloc ]? performSelector :customSelector? withObject :0];

}

注意:updated at 20120606

1.如果使用了ARC會(huì)產(chǎn)生“performSelector may cause a leak because its selector is unknown”警告
2.這種方式當(dāng)傳入一個(gè)不符合約定的消息時(shí)會(huì)繼續(xù)運(yùn)行并不報(bào)錯(cuò)。例如應(yīng)該傳入2個(gè)參數(shù),但只傳入1個(gè)參數(shù)。或傳入了3個(gè)參數(shù),第三個(gè)參數(shù)不會(huì)被初始化。

還有一種調(diào)用其他Class Function的方法是,但是不能有參數(shù),我們這里假設(shè)沒有參數(shù),那么就可以這樣

[mfs?customSelector];?

完整的代碼:

@implementation ?ClassForSelectors
- ( void ) fooNoInputs {

NSLog( @"Does nothing" );

}

- ( void ) fooOneIput:(NSString*) first {

NSLog( @"Logs %@" , first);

}

- ( void ) fooFirstInput:(NSString*) first secondInput:(NSString*) second {

NSLog( @"Logs %@ then %@" , first, second);

}

?

- ( NSArray ?* )abcWithAAA: ( NSNumber ?*)number {

int ?primaryKey = [number? intValue ];

NSLog ( "%i" , primaryKey);

}

?

- ( void ) performMethodsViaSelectors {

[ self ?performSelector: @selector (fooNoInputs)];

[ self ?performSelector: @selector (fooOneInput:) withObject: @"first" ];

[ self ?performSelector: @selector (fooFirstInput:secondInput:) withObject: @"first" withObject: @"second" ];

}

?

- ( void ) performDynamicMethodsViaSelectors {

MethodForSelectors ?* mfs = [ MethodForSelectors ? alloc ];

NSArray ?*Arrays = [ NSArray ? arrayWithObjects : @"AAA" ,? @"BBB" ,? nil ];

for ?(? NSString ?*array? in ?Arrays ){

SEL ?customSelector =? NSSelectorFromString ([ NSString stringWithFormat : @"abcWith%@:" , array]);

mfs?= [[ MethodForSelectors ? alloc ]? performSelector :customSelector? withObject :0];

}

}

@end

?
@implementation ?MethodForSelectors

- ( void )abcWithAAA: ( NSNumber ?*)number {

NSLog ( "%i" , number);

}

@end

?
?
--EOF--
作者:Buro#79xxd 出處:http://www.cnblogs.com/buro79xxd/ 文章版權(quán)歸本人所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
?
?

Objective-C中一種消息處理方法performSelector: withObject:


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 四虎免费播放观看在线视频 | 久99久视频 | 欧美成人三级一区二区在线观看 | 国产一级特黄老妇女大片免费 | 九九久久99综合一区二区 | 久久久久国产精品免费免费不卡 | 91中文字幕yellow字幕网 | 天天天天干 | 亚洲短视频在线观看 | 久久99热国产这有精品 | 99久久国产综合精品麻豆 | 欧美成人亚洲高清在线观看 | 一区二区三区免费精品视频 | 久久久久一级片 | 欧做爰xxxⅹ性欧美大片孕妇 | 精彩视频一区二区三区 | 夜色福利一区二区三区 | 国产成人永久在线播放 | 天天干天天天天 | 国产va免费高清在线观看 | 牛牛影视午夜免费福利 | 女人18一级特级毛片免费看 | 亚洲欧洲尹人香蕉综合 | 久久精品国产一区二区三区肥胖 | 九九精品在线 | 99精品国产免费久久国语 | 亚洲成人高清在线 | 久久青草社区 | 欧美成人免费在线视频 | 久久骚| 四虎影视永久地址 | 亚洲国产成人资源在线桃色 | 97午夜精品 | 免费的一级毛片 | 国产伦精品一区二区三区免 | 久久精品国产丝袜 | 九色综合网 | 日韩 三级 | 黄片毛片一级片 | 中文字幕亚洲一区二区三区 | 国产热热|