![](/img/trans.png)
[英]RACObserve(), RAC() - how to set a BOOL value based on an NSString
[英]reactivecocoa rac binding bool array to set mutilple uilabel text
我有一個 statusArray 並顯示 UILabel 文本
基於布爾值
比如說,1 顯示 someString 然后 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";
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.