我使用ReactiveCocoa记录数组中的抽头:
@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如何将这个信号转换成阵列信号?
每次发送新值时,我都需要更新数组。
发布于 2015-07-27 10:36:31
感谢MichałCiuba的回答。使用-collect只会在上游信号完成后发送收集值。
我找到了一种使用-scanWithStart:reduce:生成信号的方法,每次发送next值时,该信号都会发送集合。
RAC(self, first10Taps) = [firstTapSetSignal scanWithStart:[NSMutableArray array] reduce:^id(NSMutableArray *collectedValues, id next) {
[collectedValues addObject:(next ?: NSNull.null)];
return collectedValues;
}];发布于 2015-07-27 07:45:11
在RACSignal上有一个叫做collect的方法。它的文件说:
将所有接收方的
next收集到NSArray中。零值将转换为NSNull。这与Rx中的ToArray方法相对应。返回一个信号,该信号在接收器成功完成时发送单个NSArray。
因此,您可以简单地使用:
RAC(self, first10Taps) = [first10TapSignal collect];https://stackoverflow.com/questions/31639063
复制相似问题