我想反复调用函数foo,但是nstimer不工作。你能帮助我吗?
@implementation abc
-(id)init
{
[NSTimer scheduledTimerWithTimeInterval:(1/16) target:self selector:@selector(foo) userInfo:nil repeats:YES];
}
这是我的主要
int main(int argc, char *argv[])
{
abc *ab=[[abc alloc]init];
[NSThread sleepForTimeInterval:100];
[ab release];
return 0;
}
答案 0 :(得分:6)
由于[NSTimer scheduledTimerWithTimeInterval:...]
(reference)声明:
创建并返回一个新的NSTimer对象并在其上安排它 默认模式下的当前运行循环。
... 没有跑圈。
例如,您可以创建NSApplication
的实例(即适当的Cocoa应用程序)以使其工作(您可以创建自己的运行循环而无需创建Cocoa应用程序,但它可能是相当多的工作)。
答案 1 :(得分:-1)
尝试这行代码(1.0 / 16而不是1/16):
[NSTimer scheduledTimerWithTimeInterval:(1.0/16) target:self selector:@selector(foo) userInfo:nil repeats:YES];