gpt4 book ai didi

ios - reactivecocoa rac绑定(bind)bool数组设置多个uilabel文本

转载 作者:行者123 更新时间:2023-11-29 05:21:28 25 4
gpt4 key购买 nike

我有一个 statusArray 并显示 UILabel 文本

基于 bool 值

比如说,1 显示一些字符串然后 0 显示一些其他字符串

我应该如何使用 rac 作为 ReactiveCocoa ?

最佳答案

我暂时只想到两种办法:

  1. 监听bool变量

您需要使用 bool 变量作为属性。

@weakify(self);
[RACObserve(self, bool) subscribeNext:^(id _Nullable x) {
@strongify(self);
self.label.text = [x boolValue] ? @"a" : @"b";
}];

这会导致循环引用,所以使用了weakifystrongify,它们也是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")
  1. 绑定(bind)UILabel.text,将bool信号转换为string赋值
RAC(self.label, text) = [RACObserve(self, bool) map:^id _Nullable(id  _Nullable value) {
return [value boolValue] ? @"a" : @"b";
}];

关于ios - reactivecocoa rac绑定(bind)bool数组设置多个uilabel文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58654792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com