Created
November 29, 2011 17:49
-
-
Save wbyoung/1405696 to your computer and use it in GitHub Desktop.
Macro for safely call nil block
This file contains hidden or 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
// gcc test.m -o test -framework Foundation | |
#import <Foundation/Foundation.h> | |
#define safeblock(block) ((typeof(block))(block ? ((id)block) : ((id)^{}))) | |
int main() { | |
void (^block)(NSString *) = ^(NSString *anArgument){ | |
NSLog(@"Hello %@", anArgument); | |
}; | |
safeblock(block)(@"world"); | |
block = nil; | |
safeblock(block)(@"world"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment