关于performSelector用法,为什么没有预期结果,该如何处理
关于performSelector用法,为什么没有预期结果
首先,贴代码:
TestClass.h
为什么[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函数已经运行结束并返回->这样的后果就是程序退出之前无法运行到你想运行的函数。
------解决方案--------------------
换句话说,就是你想要运行的函数,运行时间在程序的生命周期之外。
------解决方案--------------------

收回上面说的话,再研究下。不是我上面说的那么一回事。
------解决方案--------------------
使用向导创建一个带UI的的项目,譬如说基于Single View Application的程序,然后在ViewController.m里面做这样的操作,就可以看到预期的结果了。
------解决方案--------------------
main 函数 上来 就 退出了
运行不到那里
------解决方案--------------------
在main()函数中这么写
do {
@autoreleasepool {
@try {
[[NSRunLoop currentRunLoop] run];
}
@catch (NSException *exception) {
DTLogError(__FUNCTION__, @"%@", exception);
}
}
} while (YES);
首先,贴代码:
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函数已经运行结束并返回->这样的后果就是程序退出之前无法运行到你想运行的函数。
------解决方案--------------------
换句话说,就是你想要运行的函数,运行时间在程序的生命周期之外。
------解决方案--------------------
收回上面说的话,再研究下。不是我上面说的那么一回事。
------解决方案--------------------
使用向导创建一个带UI的的项目,譬如说基于Single View Application的程序,然后在ViewController.m里面做这样的操作,就可以看到预期的结果了。
------解决方案--------------------
main 函数 上来 就 退出了
运行不到那里
------解决方案--------------------
在main()函数中这么写
do {
@autoreleasepool {
@try {
[[NSRunLoop currentRunLoop] run];
}
@catch (NSException *exception) {
DTLogError(__FUNCTION__, @"%@", exception);
}
}
} while (YES);