如何以最快的方式检查我的 nsarray 是否包含另一个 nsarray 元素 IOS
How to check my nsarray contains another nsarray element or not in fastest way IOS
我看到了很多答案,但没有得到我的答案。所以这就是为什么我决定 post 一个问题。如果有人能找到有用的 link 或答案会有所帮助。
这是我的字典数组:
<__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;
})
所以我想检查字段值是否包含失败值。
下面是我试过但似乎不起作用的代码:
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");
}
}
提前致谢!
这是令人恐惧的经典用例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(您不会孤单),这里有一个无 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;
break;
}
}
if (found) break;
}
}
if (found) NSLog(@"Found one.");
else NSLog(@"Didn't find one.");
我看到了很多答案,但没有得到我的答案。所以这就是为什么我决定 post 一个问题。如果有人能找到有用的 link 或答案会有所帮助。 这是我的字典数组:
<__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;
})
所以我想检查字段值是否包含失败值。 下面是我试过但似乎不起作用的代码:
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");
}
}
提前致谢!
这是令人恐惧的经典用例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(您不会孤单),这里有一个无 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;
break;
}
}
if (found) break;
}
}
if (found) NSLog(@"Found one.");
else NSLog(@"Didn't find one.");