block 好用的宏 摘自最新版YYKit,作者在原宏基础上加入了ARC判断使其更加全面

// 判断 block 是否被引用
#define lgf_HaveBlock(block, ...) if (block) { block(__VA_ARGS__); };

// block 防止强引用
#ifndef lgf_Weak
#if DEBUG
#if __has_feature(objc_arc)
#define lgf_Weak(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define lgf_Weak(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define lgf_Weak(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define lgf_Weak(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
#endif
#ifndef lgf_Strong
#if DEBUG
#if __has_feature(objc_arc)
#define lgf_Strong(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define lgf_Strong(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define lgf_Strong(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define lgf_Strong(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
#endif

// 具体使用

typedef void(^test)(NSString *ff, NSString *gg);
@interface ViewController ()
@property (copy, nonatomic) test tt;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    @lgf_Weak(self);
    self.tt = ^(NSString *ff, NSString *gg) {
        @lgf_Strong(self);
        // self...
    };
    lgf_HaveBlock(self.tt, @"111",@"222");
}



你可能感兴趣的:(block 好用的宏 摘自最新版YYKit,作者在原宏基础上加入了ARC判断使其更加全面)