你基本上会:
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我怎么能改写这个喜欢创建字典的笛卡尔积,并对其进行选择。 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];
您是否尝试过使用'filter:'?它似乎自然映射在这个计划上。 – allprog
'过滤器:'在一个流上操作,它不会以递归方式在2个流/序列中操作项目。 – zakdances