首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ReactiveCocoa将信号值收集到数组

使用ReactiveCocoa将信号值收集到数组
EN

Stack Overflow用户
提问于 2015-07-26 16:05:14
回答 2查看 776关注 0票数 0

我使用ReactiveCocoa记录数组中的抽头:

代码语言:javascript
复制
@interface ViewController : UIViewController
@property (strong, nonatomic) NSArray *first10Taps;
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
@end

@implementation ViewController
- (void) viewDidLoad
{
  [super viewDidLoad];

  RACSignal *tapSignal = [[self.tapGesture rac_gestureSignal] map:^id(UITapGestureRecognizer *sender) {
    return [NSValue valueWithCGPoint:[sender locationInView:sender.view]];
  }];

  // This signal will send the first 10 tapping positions and then send completed signal.
  RACSignal *first10TapSignal = [tapSignal take:10];

  // Need to turn this signal to a sequence with the first 10 taps.
  RAC(self, first10Taps) = [first10TapSignal ...];
}
@end

如何将这个信号转换成阵列信号?

每次发送新值时,我都需要更新数组。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-27 10:36:31

感谢MichałCiuba的回答。使用-collect只会在上游信号完成后发送收集值。

我找到了一种使用-scanWithStart:reduce:生成信号的方法,每次发送next值时,该信号都会发送集合。

代码语言:javascript
复制
RAC(self, first10Taps) = [firstTapSetSignal scanWithStart:[NSMutableArray array]  reduce:^id(NSMutableArray *collectedValues, id next) {
  [collectedValues addObject:(next ?: NSNull.null)];
  return collectedValues;
}];
票数 2
EN

Stack Overflow用户

发布于 2015-07-27 07:45:11

RACSignal上有一个叫做collect的方法。它的文件说:

将所有接收方的next收集到NSArray中。零值将转换为NSNull。这与Rx中的ToArray方法相对应。返回一个信号,该信号在接收器成功完成时发送单个NSArray。

因此,您可以简单地使用:

代码语言:javascript
复制
RAC(self, first10Taps) = [first10TapSignal collect];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31639063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档