#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; id object; [object performSelector:@selector(selectorName)]; [object selectorName];//兩種方法等效 // 動態綁定效果 SEL seletor; int a ; if (a==1){ seletor = @selector(foo); }else if (a==2){ seletor = @selector(bar); }else{ seletor = @selector(baz); } //performSelector 可能致使內存泄露 //侷限性 能傳的參數太少,僅限於對象 //利用GCD來替代 延後執行任務 [self performSelector:@selector(dosomeThing) withObject:nil afterDelay:5.0f]; //Use dispatch_after dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 *NSEC_PER_SEC)); dispatch_after(time, dispatch_get_main_queue(), ^{ [self dosomeThing]; }); //把任務放在主線程上執行 [self performSelectorOnMainThread:@selector(dosomeThing) withObject:nil waitUntilDone:NO]; dispatch_async(dispatch_get_main_queue(), ^{ [self dosomeThing]; }); /** * 若是想把任務放在另外一個線程上執行,那麼最好不要用performSelector系列方法,而是應該把任務封裝到塊裏,而後調用大中樞派發機制的相關方法來實現 * */ } -(void)selectorName{ } -(void)dosomeThing{ } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end