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

标签 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

最佳答案

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

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 值。

关于ios - 过滤器功能在这种情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597748/

相关文章:

iphone - 像书页一样弯曲 View

ios - 当我的应用程序在后台运行时,有没有办法改变一些值(value)?

ios - 避免在 ReactiveCocoa 上重复 http 请求

ios - 将 UITextView 绑定(bind)到 MutableProperty

ios - 链接 RACSignals 和回滚

ios - 无法使用 CGRectMake 调整 UIButton 的大小

ios - fatal error : Index out of range when using UITableView with multiple sections

ios - 为什么retainCount返回2

ios - 如何在导航栏中创建后退按钮

objective-c - 如何在NSTimer循环中刷新TableView Cell数据