来看一个例子: oop
#import <Foundation/Foundation.h> spa
@interface MyTst : NSObject .net
- (void) print; orm
@end get
@implementation MyTst it
- (void) print io
{ form
NSLog(@"xxxxxxxxxx"); class
} import
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "MyTst.h"
int main(int argc, char * argv[]) {
// @autoreleasepool {
// return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
// }
MyTst *myClass = [[MyTst alloc] init];
// 1 [myClass performSelector:@selector(print) withObject:nil afterDelay:0];
//2 [myClass performSelector:@selector(print) withObject:nil];
return 0;
}
上面的代码一、和2 会执行吗?
答案是:2会执行,由于performSelector:@selector(print) withObject:nil至关于向runloop发送了一个启动通知,收到这个通知后print方法会被马上执行
而1不会被执行。-performSelector:withObject:afterDelay: 方法本质上是一个timer回调,而timer须要依靠RunLoop才能运转,若是这是个非UI的程序且不手动起个RunLoop的话,程序应该直接就结束了吧,就算afterDelay:0 也没用。若是要想执行得 [[NSRunLoop currentRunLoop] run];才行。