Skip to content

Instantly share code, notes, and snippets.

@wbyoung
Created November 29, 2011 17:49
Show Gist options
  • Save wbyoung/1405696 to your computer and use it in GitHub Desktop.
Save wbyoung/1405696 to your computer and use it in GitHub Desktop.
Macro for safely call nil block
// 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