iDelusion
iDelusion

Reputation: 805

reactivecocoa rac binding bool array to set mutilple uilabel text

I have a statusArray and to show the UILabel texts

based on the Bool value

say , 1 show someString then 0 show some other string

how should I do that use rac as ReactiveCocoa ?

Upvotes: 0

Views: 103

Answers (1)

Nullable
Nullable

Reputation: 994

I only think of two ways for the time being:

  1. Listen to bool variables

You need to use the bool variable as a property.

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

This will result in circular references, so weakify and strongify are used, which are also part of ReactiveCocoa, specific definitions and usages in 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, convert bool signal to string assignment
RAC(self.label, text) = [RACObserve(self, bool) map:^id _Nullable(id  _Nullable value) {
     return [value boolValue] ? @"a" : @"b";
}];

Upvotes: 0

Related Questions