久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

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

 plumbiossom 2013-12-03

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

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

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

theReceiver是接受消息的對(duì)象類型是idtheSelector是消息名稱類型是SEL,。下邊代碼我們來(lái)看看如何來(lái)生成一個(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([NSStringstringWithFormat:@"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è)沒(méi)有參數(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];

forNSString *array in Arrays ){

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

mfs = [[MethodForSelectors allocperformSelector:customSelector withObject:0];

}

}

@end

 

@implementation MethodForSelectors

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

NSLog("%i", number);

}

@end






開(kāi)啟,、取消performselector  

[self performSelector:@selector(handleSelector:) withObject:[UIColor blueColor] afterDelay:1.5f];  // 開(kāi)啟

[NSObject cancelPreviousPerformRequestWithTarget:self selector:@selector(handleSelector:) withObject:[UIColor blueColor]]; // 取消

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多