Objective c NSTimer不工作
我希望代码每3秒钟显示一次“新版本”,但事实并非如此 汽车 main.cObjective c NSTimer不工作,objective-c,Objective C,我希望代码每3秒钟显示一次“新版本”,但事实并非如此 汽车 main.c #import <Foundation/Foundation.h> #import "Car.h" int main (int argc, const char * argv[]) { @autoreleasepool { Car *ford = [[Car alloc]init]; [NSTimer scheduledTimerWithTimeInterv
#import <Foundation/Foundation.h>
#import "Car.h"
int main (int argc, const char * argv[])
{
@autoreleasepool
{
Car *ford = [[Car alloc]init];
[NSTimer scheduledTimerWithTimeInterval:3
target:ford
selector:@selector(displayVersion)
userInfo:nil
repeats:YES];
}
return 0;
}
#导入
#进口“Car.h”
int main(int argc,const char*argv[]
{
@自动释放池
{
汽车*福特=[[Car alloc]init];
[NSTimer scheduledTimerWithTimeInterval:3
目标:福特
选择器:@选择器(显示版本)
用户信息:无
重复:是];
}
返回0;
}
这里怎么了
PS:我讨厌“你的文章没有太多的上下文来解释代码部分;请更清楚地解释你的场景”
我认为代码是对这个问题最好的解释 NSTimer依赖于正在运行的nsrunlop来运行。创建并计划计时器后,程序将立即终止,并且永远不会设置nsrunlop。通常,在Cocoa应用程序中,调用
main()
中的NSApplicationMain()
设置主运行循环并开始“旋转”它。您应该在Xcode中使用默认的Cocoa应用程序模板创建一个新项目,并在NSApplicationedLegate的-(void)ApplicationIDFinishLaunching:
方法中创建计时器
#import "Car.h"
@implementation Car
-(void)displayVersion
{
NSLog(@"New version");
}
@end
#import <Foundation/Foundation.h>
#import "Car.h"
int main (int argc, const char * argv[])
{
@autoreleasepool
{
Car *ford = [[Car alloc]init];
[NSTimer scheduledTimerWithTimeInterval:3
target:ford
selector:@selector(displayVersion)
userInfo:nil
repeats:YES];
}
return 0;
}