Skip to content

Instantly share code, notes, and snippets.

@jweinberg
Created January 8, 2013 04:35
Show Gist options
  • Save jweinberg/4481186 to your computer and use it in GitHub Desktop.
Save jweinberg/4481186 to your computer and use it in GitHub Desktop.
#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