2015-09-16 49 views
0

我嘗試關注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

你不應該返回YES和NO嗎而不是'YES和'NO嗎? – rmaddy

+0

ahhhh,你發佈它,我會給你點。太多'NSNumber *'。大聲笑。 @rmaddy –

回答

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

+1

謝謝,這是問題所在。你讀代碼很快 –

相關問題