在YYKit中看到这样的宏
#ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
#endif
#ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
#endif
然后在处理block循环引用的时候就可以这样
MyObject *obj = [[MyObject alloc] init];
self.myObj = obj;
@weakify(self)
self.myObj.test = ^(){
@strongify(self)
self.mLabel.text = @"aaa";
};
这样使用非常方便,在block中依然可以用self
但是目前尚未读懂这个宏。。希望知道的留个言。弄清楚之后也会更新此博客。
比如
//weakify
try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
//strongify
autoreleasepool{} __typeof__(object) object = weak##_##object;
这里面try{} @finally{} {}
是什么作用?不会是只为了让前面多个@?我试着把try{} @finally{} {}
去掉,autoreleasepool{}
去掉,依然可以正常使用。这两句是干什么的?
网友评论
@strongify(self)比较strongify(self)更像原生写法