如何比较ReactiveCocoa中的两个NSArrays

时间:2023-02-10 02:12:17

I have a stored NSArray. How can I observe the changes of another NSArray and compare with the stored one? It looks like:

我有一个存储的NSArray。如何观察另一个NSArray的变化并与存储的NSArray进行比较?看起来像:

RACSignal *compareArraySignal = [[RACObserve(self, arr) ...];

Thank you.

谢谢。

1 个解决方案

#1


1  

Depending on what how you actually want to compare these arrays, you'll need to Observe both arrays, combine their values and then map to a result via your comparison.

根据您实际想要比较这些数组的方式,您需要观察两个数组,合并它们的值,然后通过比较映射到结果。

RACSignal *compareArraySignal = [[RACSignal combineLatest:@[
        RACObserve(self, array1),
        RACObserve(self, array2)]]
  map:^id _Nullable(RACTuple * _Nullable value) {
    RACTupleUnpack(NSArray *array1, NSArray *array2) = value;
    return @([array1 isEqualToArray:array2]);
}];

In this example, I'm just comparing for equality, but you can do whatever is needed in the map block

在这个例子中,我只是比较相等,但你可以做任何地图块中需要的东西

#1


1  

Depending on what how you actually want to compare these arrays, you'll need to Observe both arrays, combine their values and then map to a result via your comparison.

根据您实际想要比较这些数组的方式,您需要观察两个数组,合并它们的值,然后通过比较映射到结果。

RACSignal *compareArraySignal = [[RACSignal combineLatest:@[
        RACObserve(self, array1),
        RACObserve(self, array2)]]
  map:^id _Nullable(RACTuple * _Nullable value) {
    RACTupleUnpack(NSArray *array1, NSArray *array2) = value;
    return @([array1 isEqualToArray:array2]);
}];

In this example, I'm just comparing for equality, but you can do whatever is needed in the map block

在这个例子中,我只是比较相等,但你可以做任何地图块中需要的东西