过滤功能在这种情况下不起作用?

时间:2015-09-16 00:21:33

标签: ios objective-c reactive-cocoa

我尝试从Ray学习关于ReactiveCocoa的教程,但不知怎的filter函数正在工作,因为它总是下降到subscribeNext,尽管我调试了filter函数确实去了使用return @NO分支。

#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "SearchViewController.h"

typedef NS_ENUM(NSInteger, RWTwitterInstantError) {
    RWTwitterInstantErrorAccessDenied,
    RWTwitterInstantErrorNoTwitterAccounts,
    RWTwitterInstantErrorInvalidResponse
};

static NSString * const RWTwitterInstantDomain = @"TwitterInstant";

@interface SearchViewController ()
{
    RACDisposable *requestTwiiterSubscription;
}
@property (strong, nonatomic) ACAccountStore *accountStore;
@property (strong, nonatomic) ACAccountType *twitterAccountType;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) UITextField *searchBarTextField;
@end

@implementation SearchViewController

- (UITextField *)searchBarTextField {
    if (!_searchBarTextField) {
        for (UIView *view in self.searchBar.subviews) {
            for (id deeperView in view.subviews) {
                if ([deeperView isKindOfClass:[UITextField class]]) {
                    _searchBarTextField = deeperView;
                }
            }
        }
    }
    return _searchBarTextField;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.searchBar.text = @"Co";

    __weak SearchViewController *weakSelf = self;
    self.accountStore = [[ACAccountStore alloc] init];
    self.twitterAccountType = [self.accountStore
                               accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];


    /**
     *  The then method waits until a completed event is emitted, then subscribes to the signal returned by its block parameter. 
     *  This effectively passes control from one signal to the next.
     */
    requestTwiiterSubscription = [[[[self requestAccessToTwitterSignal]
      then:^RACSignal *{
          return weakSelf.searchBarTextField.rac_textSignal;
      }]
      filter:^BOOL(NSString *textString) {
          if (textString.length >= 3) {
              return @YES;
          }
          return @NO;
      }]
     subscribeNext:^(id x) {
         NSLog(@"%@", x);
     } error:^(NSError *error) {
         NSLog(@"An error occurred: %@", error);
     }];
}

- (void)dealloc {
    [requestTwiiterSubscription dispose];
}

- (RACSignal *)requestAccessToTwitterSignal {

    // 1 - define an error
    NSError *accessError = [NSError errorWithDomain:RWTwitterInstantDomain
                                               code:RWTwitterInstantErrorAccessDenied
                                           userInfo:nil];

    // 2 - create the signal
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // 3 - request access to twitter
        [self.accountStore
         requestAccessToAccountsWithType:self.twitterAccountType
         options:nil
         completion:^(BOOL granted, NSError *error) {
             // 4 - handle the response
             if (!granted) {
                 [subscriber sendError:accessError];
             } else {
                 [subscriber sendNext:nil];
                 [subscriber sendCompleted];
             }
         }];
        return nil;
    }];
}

@end

1 个答案:

答案 0 :(得分:2)

过滤器中的返回值错误。你想要:

requestTwiiterSubscription = [[[[self requestAccessToTwitterSignal]
  then:^RACSignal *{
      return weakSelf.searchBarTextField.rac_textSignal;
  }]
  filter:^BOOL(NSString *textString) {
      if (textString.length >= 3) {
          return YES;
      }
      return NO;
  }]
 subscribeNext:^(id x) {
     NSLog(@"%@", x);
 } error:^(NSError *error) {
     NSLog(@"An error occurred: %@", error);
 }];

您返回NSNumber个对象而不是BOOL个值。

相关问题