NSTimer
我在 Objective-C 中有问题。这是我的源代码:Main.m
#import <Foundation/Foundation.h>
#import "TimerTest.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
TimerTest *timerTest = [[[TimerTest alloc] init] autorelease];
}
return 0;
}
定时器测试.h
#import <Foundation/Foundation.h>
@interface TimerTest : NSObject {
NSTimer *_timer;
}
@property (nonatomic, retain) NSTimer *timer;
- (id) init;
@end
定时器测试.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 秒记录一次“Tick!\n”。但是后来我的程序完成了,xcode 控制台清晰了,也就是说NSLog
in
-(void)tick:(NSDate *)dt
method didn't work 。我的错误在哪里?