Objective-C中的NSTimer
有问题。这是我的源代码: 的main.mNSTimer在xCode中无法在CMD项目中工作
#import <Foundation/Foundation.h>
#import "TimerTest.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
TimerTest *timerTest = [[[TimerTest alloc] init] autorelease];
}
return 0;
}
TimerTest.h
#import <Foundation/Foundation.h>
@interface TimerTest : NSObject {
NSTimer *_timer;
}
@property (nonatomic, retain) NSTimer *timer;
- (id) init;
@end
TimerTest.m
#import "TimerTest.h"
@implementation TimerTest
@synthesize timer = _timer;
- (id) init {
if (self = [super init]) {
[NSTimer timerWithTimeInterval:0.5f
target:self
selector:@selector(tick:)
userInfo:nil
repeats:YES];
}
return self;
}
- (void) tick: (NSDate *) dt {
NSLog(@"Tick! \n");
}
- (void) dealloc {
self.timer = nil;
[super dealloc];
}
@end
我的程序应该记录每0.5秒 “嘀\ n”! 。但后来我的程序完成了,xcode控制台很清楚,这就是NSLog
在 -(void)tick:(NSDate *)dt
方法没有用。我的错误在哪里?