├── NSTimer+Blocks.h ├── NSTimer+Blocks.m └── README.md /NSTimer+Blocks.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSTimer+Blocks.h 3 | // 4 | // Created by Jiva DeVoe on 1/14/11. 5 | // Copyright 2011 Random Ideas, LLC. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | @interface NSTimer (Blocks) 11 | +(id)scheduledTimerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats; 12 | +(id)timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats; 13 | @end 14 | -------------------------------------------------------------------------------- /NSTimer+Blocks.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSTimer+Blocks.m 3 | // 4 | // Created by Jiva DeVoe on 1/14/11. 5 | // Copyright 2011 Random Ideas, LLC. All rights reserved. 6 | // 7 | 8 | #import "NSTimer+Blocks.h" 9 | 10 | @implementation NSTimer (Blocks) 11 | 12 | +(id)scheduledTimerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats 13 | { 14 | void (^block)() = [inBlock copy]; 15 | id ret = [self scheduledTimerWithTimeInterval:inTimeInterval target:self selector:@selector(jdExecuteSimpleBlock:) userInfo:block repeats:inRepeats]; 16 | [block release]; 17 | return ret; 18 | } 19 | 20 | +(id)timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats 21 | { 22 | void (^block)() = [inBlock copy]; 23 | id ret = [self timerWithTimeInterval:inTimeInterval target:self selector:@selector(jdExecuteSimpleBlock:) userInfo:block repeats:inRepeats]; 24 | [block release]; 25 | return ret; 26 | } 27 | 28 | +(void)jdExecuteSimpleBlock:(NSTimer *)inTimer; 29 | { 30 | if([inTimer userInfo]) 31 | { 32 | void (^block)() = (void (^)())[inTimer userInfo]; 33 | block(); 34 | } 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | README 2 | ====== 3 | 4 | Extremely simple category on NSTimer which makes it able to use blocks. 5 | 6 | HOW IT WORKS 7 | ------------ 8 | 9 | I figure if you're using a block, you probably won't need to pass any userinfo object into the timer... you can get to whatever you need just from the block. So, I just hijack the `+scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:` class method and it's brother `-init...`, setting it to use itself as the target, and to execute the class method `+jdExecuteSimpleBlock:` (which is part of the category) then pass the block you specify as the userInfo object which is then uses in the execute method. Pretty straightforward stuff. 10 | 11 | HOW TO USE IT 12 | ------------- 13 | 14 | Very simple: 15 | 16 | [NSTimer scheduledTimerWithTimeInterval:2.0 block:^ 17 | { 18 | [someObj doSomething]; 19 | [someOtherObj doSomethingElse]; 20 | // ... etc ... 21 | } repeats:NO]; 22 | 23 | This may be overkill for most NSTimer operations... I mean, do you really have a need for a block? Couldn't you use the selector methods? Sure you could... but sometimes it's more elegant to use a block... so here you go. 24 | 25 | FIND THIS USEFUL? 26 | ----------------- 27 | 28 | Why don't you buy me a beer with bitcoin? 29 | 30 | My wallet address is: 1JtjkQFmJmbMumWquh5J2fuRuD6LkWr5QL 31 | 32 | ![Wallet QR Code](http://www.random-ideas.net/qrcode.png) 33 | 34 | 35 | LICENSE 36 | ------- 37 | 38 | Copyright (C) 2011 by Random Ideas, LLC 39 | 40 | Permission is hereby granted, free of charge, to any person obtaining a copy 41 | of this software and associated documentation files (the "Software"), to deal 42 | in the Software without restriction, including without limitation the rights 43 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 44 | copies of the Software, and to permit persons to whom the Software is 45 | furnished to do so, subject to the following conditions: 46 | 47 | The above copyright notice and this permission notice shall be included in 48 | all copies or substantial portions of the Software. 49 | 50 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 51 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 52 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 53 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 54 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 55 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 56 | THE SOFTWARE. 57 | --------------------------------------------------------------------------------