Skip to content
Browse files

Pass a block to the callback

A basic functioning version.

Adds + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(timerCallback_t)block;
  • Loading branch information...
1 parent 553a03c commit 4b3ebe471297f99ad7f7924c4ad3745fe7c82af6 @Abizern committed Aug 26, 2011
Showing with 55 additions and 0 deletions.
  1. +19 −0 NSTimer+JCSBlocks.h
  2. +36 −0 NSTimer+JCSBlocks.m
View
19 NSTimer+JCSBlocks.h
@@ -0,0 +1,19 @@
+//
+// NSTimer+JCSBlocks.h
+// atime
+//
+// Created by Abizer Nasir on 26/08/11
+// Copyright 2011 Jungle Candy Software. All rights reserved.
+//
+// Additions to NSTimer that allow it to use blocks as callbacks
+// Requires standard PCH macros for ZAssert()
+
+#import <Foundation/NSTimer.h>
+
+typedef void (^timerCallback_t)();
+
+@interface NSTimer (JCSBlocks)
+
++ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(timerCallback_t)block;
+
+@end
View
36 NSTimer+JCSBlocks.m
@@ -0,0 +1,36 @@
+//
+// NSTimer+JCSBlocks.m
+// atime
+//
+// Created by Abizer Nasir on 26/08/11
+// Copyright 2011 Jungle Candy Software. All rights reserved.
+//
+
+#import "NSTimer+JCSBlocks.h"
+
+@interface NSTimer (JCSBlocks_Private)
+
++ (void)handleBlockWithTimer:(NSTimer *)timer;
+
+@end
+
+@implementation NSTimer (JCSBlocks)
+
++ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(timerCallback_t)block {
+ return [self scheduledTimerWithTimeInterval:seconds target:self selector:@selector(handleBlockWithTimer:) userInfo:[[block copy] autorelease] repeats:repeats];
+
+}
+
+
+@end
+
+@implementation NSTimer (JCSBlocks_Private)
+
++ (void)handleBlockWithTimer:(NSTimer *)timer {
+ ZAssert(([timer isValid] && [timer userInfo]), @"the timer is not valid");
+
+ timerCallback_t block = [timer userInfo];
+ block();
+}
+
+@end

0 comments on commit 4b3ebe4

Please sign in to comment.
Something went wrong with that request. Please try again.