I came across an awkward situation where I would like to have a class with an NSTimer instance variable that repeatedly calls the class method while the class is alive. To illustrate, this might look like this:
@interface MyClock : NSObject {
NSTimer* _myTimer;
}
- (void)timerTick;
@end
-
@implementation MyClock
- (id)init {
self = [super init];
if (self) {
_myTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerTick) userInfo:nil repeats:NO] retain];
}
return self;
}
- (void)dealloc {
[_myTimer invalidate];
[_myTImer release];
[super dealloc];
}
- (void)timerTick {
}
@end
What I want. I do not want to expose an interface in my class to start and stop the internal timer, I just want it to work during the existence of the class. It seems simple enough.
, NSTimer . , , , , . . NSTimer, , - ivar, , ivar .
- MyClock, , . , , , , , , . -- , ... ? .
, , . NSTimer, NSTimer, . , .