@weakify, @strongify
来源:互联网 发布:m1协同软件 编辑:程序博客网 时间:2024/06/11 16:04
【http://www.jianshu.com/p/3d6c4416db5e】
今天来讲weakify strongify。
首先看一下实现的方式:
方式1: 传统写法:
#ifndef weakify#if __has_feature(objc_arc)#define weakify( x ) \\_Pragma("clang diagnostic push") \\_Pragma("clang diagnostic ignored \\"-Wshadow\\"") \\autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \\_Pragma("clang diagnostic pop")#else#define weakify( x ) \\_Pragma("clang diagnostic push") \\_Pragma("clang diagnostic ignored \\"-Wshadow\\"") \\autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \\_Pragma("clang diagnostic pop")#endif#endif#ifndef strongify#if __has_feature(objc_arc)#define strongify( x ) \\_Pragma("clang diagnostic push") \\_Pragma("clang diagnostic ignored \\"-Wshadow\\"") \\try{} @finally{} __typeof__(x) x = __weak_##x##__; \\_Pragma("clang diagnostic pop")#else#define strongify( x ) \\_Pragma("clang diagnostic push") \\_Pragma("clang diagnostic ignored \\"-Wshadow\\"") \\try{} @finally{} __typeof__(x) x = __block_##x##__; \\_Pragma("clang diagnostic pop")#endif#endif
第二种: 装逼写法: 比如RAC
#define weakify(...) \\ autoreleasepool {} \\ metamacro_foreach_cxt(rac_weakify_,, __weak, __VA_ARGS__)#define strongify(...) \\ try {} @finally {} \\ _Pragma("clang diagnostic push") \\ _Pragma("clang diagnostic ignored \\"-Wshadow\\"") \\ metamacro_foreach(rac_strongify_,, __VA_ARGS__) \\ _Pragma("clang diagnostic pop")
今天, 来解释一下RAC怎么实现这种装逼的写法。
他们的作用主要是在block内部管理对self的引用:
@weakify(self); // 定义了一个__weak的self_weak_变量 [RACObserve(self, name) subscribeNext:^(NSString *name) { @strongify(self); // 局域定义了一个__strong的self指针指向self_weak self.outputLabel.text = name; }];
这个宏为什么这么吊,前面加@,其实就是一个啥都没干的@autoreleasepool {}前面的那个@,为了显眼罢了。 还有metamacro_foreach_cxt, 我们一层一层的往里点
第一次:
#define metamacro_foreach_cxt(MACRO, SEP, CONTEXT, ...) \\ metamacro_concat(metamacro_foreach_cxt, metamacro_argcount(__VA_ARGS__))(MACRO, SEP, CONTEXT, __VA_ARGS__)
继续点下去
#define metamacro_concat(A, B) \\ metamacro_concat_(A, B)
接着点
#define metamacro_concat_(A, B) A ## B
到最后, 才tm的发现, 这不就是个C语言中组合运算符的么, 把2个运算符组合成为1个运算符。 然后回过头看, 他就是吧weak 以及第二步骤中`#define rac_weakify_(INDEX, CONTEXT, VAR) \
CONTEXT typeof__(VAR) metamacro_concat(VAR, weak) = (VAR);`的typedef拼接进去- - , 妈的 这都要装逼。。。。
这两个宏一定成对出现,先weak再strong.可以很好的管理Block内部对self的引用。 当然你如果是一个不爱用黄色宏的屌丝的话、 你可以用原生代码写出来
__weak typeof(self) weakSelf = self; self.Button.rac_command = [[RACCommand alloc] initWithEnabled:textSig signalBlock:^RACSignal *(NSString * input) { __strong typeof(weakSelf) strongSelf = weakSelf; return nil; }];
原文地址http://www.cocoachina.com/industry/20140621/8905.html
越是细节的东西, 越注定成败。 @Dylan.
0 0
- @weakify, @strongify
- @weakify, @strongify
- @weakify, @strongify
- weakify和strongify
- @weakify, @strongify 详解
- @weakify和@strongify
- @weakify, @strongify 使用
- weakify strongify 实现
- 剖析RAC中的@weakify、@strongify
- MRC/ARC通用的weakify和strongify
- Reactive Cocoa中@weakify和@strongify实现分析
- 文MRC/ARC通用的weakify和strongify
- weakify/strongify 大法,的简单宏,抄至于YYKit
- ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- @strongify以及@weakify宏命令的的使用(1)
- ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- MyEclipse:详细使用教程
- Maven 的搭建
- lua排序
- 软件工程
- 安卓自学——ViewPager与FragmentTabHost实现拖动翻页
- @weakify, @strongify
- android Contentprovider配置记录
- android的Drawable详解
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- super和this关键字
- 用LuaBridge为Lua绑定C/C++对象
- 函数调用约定
- AOSP (一):获取源码
- 库函数建立工程时容易犯的错误