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