如果self为零,则Objective-C@strong返回

如果self为零,则Objective-C@strong返回,objective-c,macros,reactive-cocoa,Objective C,Macros,Reactive Cocoa,我使用weakify/strongify宏,我想将下一个逻辑移到宏 @weakify(self); BOOL (^matchesFooOrBar)(id) = ^ BOOL (id obj){ @strongify(self); if (self == nil) return; // I want to move it to the macros strongify定义: #define strongify(...) \ rac_keywordify \ _Pr

我使用weakify/strongify宏,我想将下一个逻辑移到宏

@weakify(self);
BOOL (^matchesFooOrBar)(id) = ^ BOOL (id obj){
    @strongify(self);
    if (self == nil) return; // I want to move it to the macros
strongify
定义:

#define strongify(...) \
    rac_keywordify \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Wshadow\"") \
    metamacro_foreach(rac_strongify_,, __VA_ARGS__) \
    _Pragma("clang diagnostic pop")

有什么建议吗?

我花了一些时间想清楚

#define myStrongify(first, ...) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
metamacro_foreach(rac_strongify_,, __VA_ARGS__) \
if (!first) { NS_VALUERETURN(NO, BOOL); } \
_Pragma("clang diagnostic pop")
用法如下:

BOOL (^matchesFooOrBar)(id) = ^BOOL(id obj) {
    NSLog(@"before");
    myStrongify(obj);
    NSLog(@"after");

    // your custom logic here
    ...

    return YES;
};

仅供参考,来自苹果公司文档。请随意测试。

最好在strongify中添加第二个参数,这是null的默认返回值。