比较两个数组里面的内容是否相同,代码如下:
NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; NSArray *array2 = [NSArray arrayWithObjects:@"d",@"e", nil]; bool bol = false; //创建俩新的数组 NSMutableArray *oldArr = [NSMutableArray arrayWithArray:array1]; NSMutableArray *newArr = [NSMutableArray arrayWithArray:array2]; //对array1排序。 [oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){ return obj1 > obj2; }]; //对array2排序。 [newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){ return obj1 > obj2; }]; if (newArr.count == oldArr.count) { bol = true; for (int i = 0; i < oldArr.count; i++) { id c1 = [oldArr objectAtIndex:i]; id newc = [newArr objectAtIndex:i]; if (![newc isEqualToString:c1]) { bol = false; break; } } } if (bol) { NSLog(@"两个数组的内容相同!"); } else { NSLog(@"两个数组的内容不相同!"); }
打印如下:
然后再在合适的场景下进行操作。