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
方法沒有用。我的錯誤在哪裏?