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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to