假设我有 2 个我事先不知道的 NSDictionaries
,例如:
NSDictionary *dictA = @{ @"key1" : @1,
@"key2" : @2 };
NSDictionary *dictB = @{ @"key1" : @"a string" };
我想找到 dictB
的键和 dictA
的键或值之间的第一个匹配项。 dictB
的每个键要么是一个 NSNumber 要么是一个字符串。如果是数字,尝试从 dictA
的 values 中找到匹配项。如果是字符串,尝试从 dictA
的 keys 中找到匹配项。
使用 for 循环,它看起来像这样:
id match;
for (id key in dictA ) {
for (id _key in dictB {
if ( [_key is kindOfClass:NSNumber.class] && _key == dictA[key] ) {
match = _key
goto outer;
}
else if ( [_key is kindOfClass:NSString.class] && [_key isEqualToString:key] ) {
match = _key
goto outer;
}
}
};
outer:;
NSString *message = match ? @"A match was found" : @"No match was found";
NSLog(message);
我如何使用 RACSequence
和 RACStream
方法用 ReactiveCocoa 重写它,使其看起来像这样:
// shortened pseudo code:
// id match = [dictA.rac_sequence compare with dictB.rac_sequence using block and return first match];
最佳答案
基本上,您想要创建字典的笛卡尔积并在其上进行选择。据我所知,ReactiveCocoa 中没有默认运算符可以为您执行此操作。 (在 LINQ 中有用于此操作的运算符。)在 RAC 中,最简单的解决方案是使用 scanWithStart:combine:
方法来实现此操作。笛卡尔坐标准备就绪后,filter:
和 take:1
操作将生成您选择的序列。
NSDictionary *adic = @{@"aa":@"vb", @"ab": @"va"};
NSDictionary *bdic = @{@"ba": @"va", @"bb":@"vb"};;
RACSequence *aseq = adic.rac_keySequence;
RACSequence *bseq = bdic.rac_keySequence;
RACSequence *cartesian = [[aseq scanWithStart:nil combine:^id(id running, id next_a) {
return [bseq scanWithStart:nil combine:^id(id running, id next_b) {
return RACTuplePack(next_a, next_b);
}];
}] flatten];
RACSequence *filteredCartesian = [cartesian filter:^BOOL(RACTuple *value) {
RACTupleUnpack(NSString *key_a, NSString *key_b) = value;
// business logic with keys
return false;
}];
RACSequence *firstMatch = [filteredCartesian take:1];
关于objective-c - 如何使用 ReactiveCocoa 简化我的嵌套 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18551212/