首先,贴代码:
TestClass.h#import <Foundation/Foundation.h>@interface TestClass : NSObject-(void) outputHelloWorld;
-(void) outputHelloWorldInSeconds;
@end#import "TestClass.h"@implementation TestClass-(void) outputHelloWorld
{
NSLog(@"HelloWord, boy");
}-(void) outputHelloWorldInSeconds
{
[self performSelector:@selector(outputHelloWorld) withObject:nil afterDelay:1];
NSLog(@"outputHelloWorldInSeconds########");
sleep(3);
NSLog(@"outputHelloWorldInSeconds--------");
}
@end#import <Foundation/Foundation.h>
#import "TestClass.h"int main (int argc, const char * argv[])
{ @autoreleasepool
{
TestClass *testObject = [[TestClass alloc] init];
// [testObject outputHelloWord];
[testObject outputHelloWorldInSeconds];
// [testObject release];
}
return 0;
}
为什么[self performSelector:@selector(outputHelloWorld) withObject:nil afterDelay:1];好象没有执行到,没有预期结果?
最后控制台的输出是:
sharedlibrary apply-load-rules all
[Switching to process 650 thread 0x0]
2013-05-22 04:39:20.191 helloworld[650:707] outputHelloWorldInSeconds########
2013-05-22 04:39:23.195 helloworld[650:707] outputHelloWorldInSeconds--------
Program ended with exit code: 0
TestClass.h#import <Foundation/Foundation.h>@interface TestClass : NSObject-(void) outputHelloWorld;
-(void) outputHelloWorldInSeconds;
@end#import "TestClass.h"@implementation TestClass-(void) outputHelloWorld
{
NSLog(@"HelloWord, boy");
}-(void) outputHelloWorldInSeconds
{
[self performSelector:@selector(outputHelloWorld) withObject:nil afterDelay:1];
NSLog(@"outputHelloWorldInSeconds########");
sleep(3);
NSLog(@"outputHelloWorldInSeconds--------");
}
@end#import <Foundation/Foundation.h>
#import "TestClass.h"int main (int argc, const char * argv[])
{ @autoreleasepool
{
TestClass *testObject = [[TestClass alloc] init];
// [testObject outputHelloWord];
[testObject outputHelloWorldInSeconds];
// [testObject release];
}
return 0;
}
为什么[self performSelector:@selector(outputHelloWorld) withObject:nil afterDelay:1];好象没有执行到,没有预期结果?
最后控制台的输出是:
sharedlibrary apply-load-rules all
[Switching to process 650 thread 0x0]
2013-05-22 04:39:20.191 helloworld[650:707] outputHelloWorldInSeconds########
2013-05-22 04:39:23.195 helloworld[650:707] outputHelloWorldInSeconds--------
Program ended with exit code: 0
还没有执行到该函数,main函数已经运行结束并返回->这样的后果就是程序退出之前无法运行到你想运行的函数。
运行不到那里
@autoreleasepool {
@try {
[[NSRunLoop currentRunLoop] run];
}
@catch (NSException *exception) {
DTLogError(__FUNCTION__, @"%@", exception);
}
}
} while (YES);