@weakify, @strongify 详解
来源:互联网 发布:中英文翻译软件 编辑:程序博客网 时间:2024/05/16 11:05
http://www.jianshu.com/p/3d6c4416db5e
今天来讲weakify strongify。首先看一下实现的方式:方式1: 传统写法:#ifndef weakify# __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")#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# __has_feature(objc_arc)#define strongify( x ) \_Pragma("clang diagnostic push") \_Pragma("clang diagnostic ignored \"-Wshadow\"") \{} @finally{} __typeof__(x) x = __weak_##x##__; \_Pragma("clang diagnostic pop")#define strongify( x ) \_Pragma("clang diagnostic push") \_Pragma("clang diagnostic ignored \"-Wshadow\"") \{} @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(...) \ {} @finally {} \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wshadow\"") \ metamacro_foreach(rac_strongify_,, __VA_ARGS__) \ _Pragma("clang diagnostic pop")今天, 来解释一下RAC怎么实现这种装逼的写法。他们的作用主要是在block内部管理对self的引用:@weakify(); 定义了一个__weak的self_weak变量 [RACObserve(, name) subscribeNext:^(NSString *name) { @strongify(); 局域定义了一个__strong的指针指向self_weak .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到最后, 才tm的发现, 这不就是个C语言中组合运算符的么, 把2个运算符组合成为1个运算符。 然后回过头看, 他就是吧weak 以及第二步骤中`#define rac_weakify_(INDEX, CONTEXT, VAR) \CONTEXT typeof__(VAR) metamacro_concat(VAR, weak) = (VAR);`的typedef拼接进去- - , 妈的 这都要装逼。。。。这两个宏一定成对出现,先weak再strong.可以很好的管理Block内部对self的引用。 当然你如果是一个不爱用黄色宏的屌丝的话、 你可以用原生代码写出来 __ typeof() weakSelf = ; .Button.rac_command = [[RACCommand alloc] initWithEnabled:textSig signalBlock:^RACSignal *(NSString * input) { __strong typeof(weakSelf) strongSelf = weakSelf; return ; }];越是细节的东西, 越注定成败。 @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
- Android自定义控件模仿iOS滑块SwitchButton
- java synchronized详解
- C++11 新特性
- ViewPager详解(三)——自动轮播图片小于三张的问题解决
- j2EE连接数据库的增删改查操作
- @weakify, @strongify 详解
- shell之进程管理
- .net特性
- Spark Shuffle过程理解
- C++异常处理(多态实现机制)
- Mybatis多对多关联查询
- Java初始化顺序
- geant4 binContent相关应用 一
- 单源最短路径-Bellman-Ford算法