weakify/strongify 大法,的简单宏,抄至于YYKit

来源:互联网 发布:工业网络就业前景 编辑:程序博客网 时间:2024/05/19 23:01


#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

看了一下AFN 经常看到这样的语句

 __weak__typeof(self)weakSelf =self;

    AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {

        __strong__typeof(weakSelf)strongSelf = weakSelf;


        strongSelf.networkReachabilityStatus = status;

        if (strongSelf.networkReachabilityStatusBlock) {

            strongSelf.networkReachabilityStatusBlock(status);

        }


    };

等同于导入宏后的

@weakify (self)

    AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {

       

@strongify (self)

        self.networkReachabilityStatus = status;

        if (self.networkReachabilityStatusBlock) {

            self.networkReachabilityStatusBlock(status);

        }


    };




0 0
原创粉丝点击