0

I have seen many of the answers but didn't get my answer. So that's why I decide to post a question. If anybody can find the helpful link or answer will be helpful. Here is my array of dictionary:

<__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;
})  

So I want to check if the fieldvalue contains failvalues or not. Below is the code which I have tried but it doesn't seem to work:

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");
            }
        }  

EDIT:
This one I have tried but how to break both the loops

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");
                }
        }

Thanks in advance!

Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
  • What do you want in the end? `True`/`false` if there is any failed values? A list of all the failed values? Only the `failValues` that have a corresponding `fieldValue`? – Larme Dec 11 '20 at 09:40
  • https://pastebin.com/7MRFUjAU ? – Larme Dec 11 '20 at 09:52
  • https://pastebin.com/zNMBB7en – TienLe Dec 11 '20 at 10:01
  • If it contains then I want to set one boolean true and break both the loops – Mihir Oza Dec 11 '20 at 12:15
  • Please check my updated questions. Thanks! – Mihir Oza Dec 11 '20 at 12:16
  • The topic title is a bit confusing, but I think you want something like [this](https://stackoverflow.com/questions/3710094/how-to-check-if-the-value-in-an-nsdictionary-exists-in-an-array-of-dictionarys). – koen Dec 11 '20 at 20:22
  • Use fast iteration: for (field in airfields) ... An awful lot faster than iterating by hand. And don't evaluate the same expression ten times in a row. – gnasher729 Dec 11 '20 at 20:57
  • @gnasher729 Yes Thanks, Tyr has given the solution with fast iteration. – Mihir Oza Dec 15 '20 at 13:07

1 Answers1

1

This is a classic use case for the feared and dreaded goto. It's well known that goto can create disastrously messy code, but in cases like this it'll make things cleaner. You have to know when to use goto, and definitely use it sparingly. But here's how I'd write your code:

    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.");

And if you cannot bring yourself to use goto (you wouldn't be alone,) here's a gotoless alternative:

    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.");
TyR
  • 718
  • 4
  • 9