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ì)象類型是id,theSelector是消息名稱類型是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]; for ( NSString *array in Arrays ){ SEL customSelector = NSSelectorFromString([NSStringstringWithFormat:@"abcWith%@:", array]); mfs = [[MethodForSelectors alloc] performSelector: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]]; // 取消 |
|
來(lái)自: plumbiossom > 《代碼》