Created
January 8, 2013 04:35
-
-
Save jweinberg/4481186 to your computer and use it in GitHub Desktop.
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> | |
#import <sys/socket.h> | |
#define WEAK_BLOCK(var, __VA_ARGS__) \ | |
(^(typeof(var) __strong var##Strong){ \ | |
__weak typeof(var) var##Weak = var##Strong; \ | |
return ^{ \ | |
typeof(var) var = var##Weak; \ | |
__VA_ARGS__ \ | |
}; \ | |
})(var) | |
@interface Foo : NSObject | |
@property (nonatomic, copy) void (^someBlock)(); | |
@end | |
@implementation Foo | |
- (id)init | |
{ | |
if ((self = [super init])) | |
{ | |
[self setSomeBlock:WEAK_BLOCK(self, | |
if (self) | |
{ | |
NSLog(@"self %@", self); | |
NSLog(@"%@", self->isa); | |
} | |
else | |
{ | |
NSLog(@"Dieded"); | |
} | |
)]; | |
} | |
return self; | |
} | |
@end | |
int main() | |
{ | |
void (^block)(void) = nil; | |
@autoreleasepool { | |
Foo *foo = [[Foo alloc] init]; | |
block = [foo someBlock]; | |
block(); | |
} | |
block(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment