如何以最快的方式检查我的 nsarray 是否包含另一个 nsarray 元素 IOS

How to check my nsarray contains another nsarray element or not in fastest way IOS

提问人:Mihir Oza 提问时间:12/11/2020 最后编辑:Mihir Oza 更新时间:12/12/2020 访问量:58

问:

我看过很多答案,但没有得到答案。所以这就是我决定发布一个问题的原因。如果有人能找到有用的链接或答案,将会有所帮助。 这是我的字典数组:

<__NSArrayM 0x283ba04e0>(
{
    failvalues =     (
        "Check 1"
    );
    fieldname = "Check 3";
    fieldvalue =     (
        "Check 1",
        "Check 2"
    );
    showtype = mandatory;
    tagvalue = 0;
},
{
    failvalues =     (
        Fail
    );
    fieldname = "Dropdown 3";
    fieldvalue =     (
        Fail
    );
    showtype = mandatory;
    tagvalue = 1;
},
{
    failvalues =     (
        "Check 1",
        "Check 4"
    );
    fieldname = "Check 4";
    fieldvalue =     (
        "Check 1",
        "Check 2"
    );
    showtype = mandatory;
    tagvalue = 2;
})  

所以我想检查 fieldvalue 是否包含 failvalues。 以下是我尝试过的代码,但它似乎不起作用:

for (int i = 0; i< [arrFields count]; i++) {
            if ([[[arrFields objectAtIndex:i]objectForKey:@"failvalues"] isKindOfClass:[NSArray class]]) {
                if (![[[[arrFields objectAtIndex:i] objectForKey:@"failvalues"] objectAtIndex:0] isEqualToString:@""]) {
                    NSLog(@"Field Values %@",[[arrFields objectAtIndex:i]objectForKey:@"fieldvalue"]);
                   NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:@"failvalues"];
                    for (int j = 0; j < [failValues count]; j++) {
                        if ([failValues containsObject:[[arrFields objectAtIndex:i]objectForKey:@"fieldvalue"]]) {
                            NSLog(@"Contains %@",[[arrFields objectAtIndex:i]objectForKey:@"fieldvalue"]);
                        } else {
                            NSLog(@"No fail values");
                        }
                    }
                } else {
                    NSLog(@"No Fail Fields");
                }
            } else {
                NSLog(@"Not an array");
            }
        }  

编辑:
这个我已经尝试过了,但如何打破这两个循环

for (int i = 0; i< [arrFields count]; i++) {
            NSArray *fieldValues = [[arrFields objectAtIndex:i]objectForKey:@"fieldvalue"];
            NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:@"failvalues"];
                if (![[[[arrFields objectAtIndex:i] objectForKey:@"failvalues"] objectAtIndex:0] isEqualToString:@""]) {
                    //NSLog(@"Field Values %@",[[arrFields objectAtIndex:i]objectForKey:@"fieldvalue"]);
                    for(NSString *value in fieldValues){
                        if ([failValues containsObject:value]) {
                            NSLog(@"Contains %@",value);
                            scanStatus = TRUE;
                            return;
                        }
                    }
                } else {
                    NSLog(@"No Fail Fields");
                }
        }

提前致谢!

iOS Objective-C Xcode nsArray

评论

0赞 Larme 12/11/2020
你到底想要什么?/ 是否有任何失败的值?所有失败值的列表?只有 that 才有相应的 ?TruefalsefailValuesfieldValue
0赞 Larme 12/11/2020
pastebin.com/7MRFUjAU
0赞 TienLe 12/11/2020
pastebin.com/zNMBB7en
0赞 Mihir Oza 12/11/2020
如果它包含,那么我想设置一个布尔值 true 并断开两个循环
0赞 Mihir Oza 12/11/2020
请查看我更新的问题。谢谢!

答:

1赞 TyR 12/12/2020 #1

这是令人恐惧和恐惧的经典用例。众所周知,goto 可以创建灾难性的混乱代码,但在这种情况下,它会使事情变得更干净。您必须知道何时使用 goto,并且绝对谨慎使用它。但这是我编写代码的方式:goto

    BOOL found = NO;
    for (NSDictionary *dict in arrFields)
    {
        NSArray *fieldValues = dict[@"fieldvalue"];
        NSArray *failValues = dict[@"failvalues"];
        if (![failValues[0] isEqualToString:@""]) {
            for (NSString *value in fieldValue) {
                if ([failValues containsObject:value]) {
                    NSLog(@"Contains %@",value);
                    found = YES;
                    goto leaveLoops;
                }
            }
        }
    }
leaveLoops:
    if (found) NSLog(@"Found one.");
    else NSLog(@"Didn't find one.");

如果你不能让自己使用 goto(你不会孤单),这里有一个无 gotos 的替代方案:

    BOOL found = NO;
    for (NSDictionary *dict in arrFields)
    {
        NSArray *fieldValues = dict[@"fieldvalue"];
        NSArray *failValues = dict[@"failvalues"];
        if (![failValues[0] isEqualToString:@""]) {
            for (NSString *value in fieldValue) {
                if ([failValues containsObject:value]) {
                    NSLog(@"Contains %@",value);
                    found = YES;
                    break;
                }
            }
            if (found) break;
        }
    }
    
    if (found) NSLog(@"Found one.");
    else NSLog(@"Didn't find one.");

评论

1赞 Mihir Oza 12/15/2020
嗨,@TyR你更喜欢哪一个?我的意思是性能方面?
0赞 Mihir Oza 12/15/2020
谢谢TyR的帮助。我认为 goto 在我的场景中更可取:)
1赞 TyR 12/16/2020
在性能方面,两者之间不会有任何实际区别。(编译的 goto 可能会通过一两条指令更快。