Last active
August 17, 2017 11:59
-
-
Save ratulSharker/3ebd95a9be45995a4787d179365b45a6 to your computer and use it in GitHub Desktop.
NSTimer does not support block until the release of ios 10. This gist can serve in that situation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// | |
// HISTORY... | |
// | |
// from ios 10 simillar functionality is given which is initialized | |
// with a block, but we need to support the older version too, | |
// thats why this class is been introduced | |
// | |
typedef void (^NSTimerExecutionBlock)(NSTimer*); | |
@interface NSTimerBlock : NSObject | |
-(instancetype)initWithInterval:(NSTimeInterval)interval withExecutionBlock:(NSTimerExecutionBlock)block repeats:(BOOL)repeat; | |
-(void)stopTimer; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSTimerBlock.h" | |
@implementation NSTimerBlock | |
{ | |
NSTimer *timer; | |
NSTimerExecutionBlock execBlock; | |
} | |
-(instancetype)initWithInterval:(NSTimeInterval)interval withExecutionBlock:(NSTimerExecutionBlock)block repeats:(BOOL)repeat | |
{ | |
self = [super init]; | |
if(self) | |
{ | |
// | |
// saving the code block, | |
// which is to be executed later | |
// | |
execBlock = block; | |
// | |
// initiate the timer | |
// | |
timer = [NSTimer scheduledTimerWithTimeInterval:interval | |
target:self | |
selector:@selector(timerFired:) | |
userInfo:nil | |
repeats:repeat]; | |
} | |
return self; | |
} | |
-(void)stopTimer | |
{ | |
[timer invalidate]; | |
timer = nil; | |
} | |
#pragma mark timer selector | |
-(void)timerFired:(NSTimer*)tmr | |
{ | |
if(execBlock) | |
{ | |
// | |
// some sort of execution block is | |
// present for processing | |
// so we will execute | |
// | |
execBlock(tmr); | |
} | |
else | |
{ | |
[tmr invalidate]; | |
tmr = nil; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you need NSTimer with Block feature ?, ios 10 NSTimer has it. But what for the previous version ? This can serve as well the same purpose with more cleaner code.
How to use::
if you want to suppress unused warning put them inside like following