为什么我的NSTimer不工作?

zzz*_*zzz 1 objective-c nstimer

我想反复调用函数foo,但是nstimer不工作.请你帮我吗?

@implementation abc
-(id)init
{
   [NSTimer scheduledTimerWithTimeInterval:(1/16) target:self selector:@selector(foo) userInfo:nil repeats:YES];
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要

int main(int argc, char *argv[])
{
    abc *ab=[[abc alloc]init];



    [NSThread sleepForTimeInterval:100];


    [ab release];

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

tro*_*foe 6

它没有用,因为[NSTimer scheduledTimerWithTimeInterval:...](参考)说明:

创建并返回一个新的NSTimer对象,并在默认模式下在当前运行循环上对其进行调度.

...而且你没有运行循环.

例如,您可以创建一个NSApplication(即适当的Cocoa应用程序)实例以使其工作(您可以创建自己的运行循环而无需创建Cocoa应用程序,但这将是相当多的工作).