Re: Could somebody please fix NSTimer?
Re: Could somebody please fix NSTimer?
- Subject: Re: Could somebody please fix NSTimer?
- From: Gordon Apple <email@hidden>
- Date: Tue, 15 Jan 2013 14:50:21 -0600
- Thread-topic: Could somebody please fix NSTimer?
In case anybody is interested, the following wrapper for NSTimer solves the
problem. (You can add the other methods yourself, if needed.)
#import <Foundation/Foundation.h>
@interface RTPTimer : NSObject
@property(nonatomic, weak) id target;
@property(nonatomic) SEL sel;
+ (RTPTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)repeats;
- (void)invalidate;
- (BOOL)isValid;
- (NSTimeInterval)timeInterval;
- (id)userInfo;
@end
#import "RTPTimer.h"
@interface RTPTimer ()
@property(nonatomic, strong) NSTimer *timer;
@end
@implementation RTPTimer
#pragma mark - Lifecycle
+ (RTPTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats {
RTPTimer *ourTimer = [[RTPTimer alloc] init];
ourTimer.sel = aSelector;
ourTimer.target = target;
ourTimer.timer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:ourTimer
selector:@selector(executeSelector:)
userInfo:userInfo
repeats:repeats];
return ourTimer;
}
- (void)dealloc {
if(self.timer != nil) {
[self.timer invalidate];
self.timer = nil;
}
}
#pragma mark - Access
- (void)invalidate {
[self.timer invalidate];
self.timer = nil;
}
- (BOOL)isValid {
return [self.timer isValid];
}
- (NSTimeInterval)timeInterval {
return [self.timer timeInterval];
}
- (id)userInfo {
return [self.timer userInfo];
}
#pragma mark - Internal
- (void) executeSelector:(NSTimer*)timer {
if(self.target != nil) {
if([self.target respondsToSelector:self.sel]) // Needed?
[self.target performSelector:self.sel withObject:self];
}
else
[self invalidate];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden