首先,贴代码:
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

解决方案 »

  1.   


    还没有执行到该函数,main函数已经运行结束并返回->这样的后果就是程序退出之前无法运行到你想运行的函数。
      

  2.   

    使用向导创建一个带UI的的项目,譬如说基于Single View Application的程序,然后在ViewController.m里面做这样的操作,就可以看到预期的结果了。
      

  3.   

    main 函数 上来 就 退出了
    运行不到那里
      

  4.   

    在main()函数中这么写 do {
    @autoreleasepool {
    @try {
    [[NSRunLoop currentRunLoop] run];
    }
    @catch (NSException *exception) {
    DTLogError(__FUNCTION__, @"%@", exception);
    }
    }
        } while (YES);