Skip to content

Instantly share code, notes, and snippets.

@faimin
Last active May 17, 2017 10:33
Show Gist options
  • Save faimin/ea02582967f4d69e26be42757287b74e to your computer and use it in GitHub Desktop.
Save faimin/ea02582967f4d69e26be42757287b74e to your computer and use it in GitHub Desktop.
resolve loop references in NSTimer
@interface NSTimer (ZDUtility)
+ (NSTimer *)zd_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
block:(void (^)(NSTimer *timer))block
repeats:(BOOL)repeats;
+ (NSTimer *)zd_timerWithTimeInterval:(NSTimeInterval)seconds
block:(void (^)(NSTimer *timer))block
repeats:(BOOL)repeats;
@end
@implementation NSTimer (ZDUtility)
#pragma mark - Public Method
+ (NSTimer *)zd_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
return [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(executeTimerBlock:) userInfo:[block copy] repeats:repeats];
}
+ (NSTimer *)zd_timerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
return [NSTimer timerWithTimeInterval:seconds target:self selector:@selector(executeTimerBlock:) userInfo:[block copy] repeats:repeats];
}
#pragma mark - Private Method
+ (void)executeTimerBlock:(NSTimer *)timer {
if ([timer userInfo]) {
void(^timerBlock)(NSTimer *) = (void(^)(NSTimer *))[timer userInfo];
timerBlock(timer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment