ios - reactivecocoa rac 绑定 bool 数组以设置多个 uilabel 文本
问题描述
我有一个 statusArray 并显示 UILabel 文本
基于布尔值
比如说,1 显示一些字符串,然后 0 显示一些其他字符串
我应该如何使用 rac 作为 ReactiveCocoa ?
解决方案
我暂时只想到两种方式:
- 听
bool
变量
您需要将bool
变量用作属性。
@weakify(self);
[RACObserve(self, bool) subscribeNext:^(id _Nullable x) {
@strongify(self);
self.label.text = [x boolValue] ? @"a" : @"b";
}];
这将导致循环引用,因此weakify
和strongify
被使用,它们也是 的一部分ReactiveCocoa
,具体定义和用法RACEXTScope.h
/**
* Creates \c __weak shadow variables for each of the variables provided as
* arguments, which can later be made strong again with #strongify.
*
* This is typically used to weakly reference variables in a block, but then
* ensure that the variables stay alive during the actual execution of the block
* (if they were live upon entry).
*
* See #strongify for an example of usage.
*/
#define weakify(...) \
rac_keywordify \
metamacro_foreach_cxt(rac_weakify_,, __weak, __VA_ARGS__)
/**
* Strongly references each of the variables provided as arguments, which must
* have previously been passed to #weakify.
*
* The strong references created will shadow the original variable names, such
* that the original names can be used without issue (and a significantly
* reduced risk of retain cycles) in the current scope.
*
* @code
id foo = [[NSObject alloc] init];
id bar = [[NSObject alloc] init];
@weakify(foo, bar);
// this block will not keep 'foo' or 'bar' alive
BOOL (^matchesFooOrBar)(id) = ^ BOOL (id obj){
// but now, upon entry, 'foo' and 'bar' will stay alive until the block has
// finished executing
@strongify(foo, bar);
return [foo isEqual:obj] || [bar isEqual:obj];
};
* @endcode
*/
#define strongify(...) \
rac_keywordify \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
metamacro_foreach(rac_strongify_,, __VA_ARGS__) \
_Pragma("clang diagnostic pop")
- 绑定
UILabel.text
,将bool
信号转换为string
赋值
RAC(self.label, text) = [RACObserve(self, bool) map:^id _Nullable(id _Nullable value) {
return [value boolValue] ? @"a" : @"b";
}];
推荐阅读
- java - StringBuffer 创建时,String 对象存储在内存中的什么位置?
- odbc - 有没有办法在 Knexjs 中连接 ODBC 数据源
- java - Gradle构建依赖表单外部go项目
- javascript - 实时 API 后端和前端
- if-statement - 如何使用 while 循环从 Kotlin 中的用户那里获得答案(是或否)
- ios - 无法在当前状态下关闭设备:关闭 Xcode 模拟器
- python-3.x - 如何使用 python scrapy 单击按钮并从网站上抓取文本
- python - 在python中将两个连续的行合并为一个具有相同唯一键的行
- php - 变量不起作用的 fsockopen
- mysql - 设置为 NULL 的外键约束字段不起作用