时间:2020-11-15 13:41:16 | 栏目:iOS代码 | 点击:次
IOS开发之判断两个数组中数据是否相同实例详解
前言:
工作中遇到的问题,这里记录下,也许能帮助到大家
实例代码:
NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil nil]; NSArray *array2 = [NSArray arrayWithObjects:@"b", @"a", @"c", nil nil]; bool bol = false; //创建俩新的数组 NSMutableArray *oldArr = [NSMutableArray arrayWithArray:array1]; NSMutableArray *newArr = [NSMutableArray arrayWithArray:array2]; //对数组1排序。 [oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){ return obj1 > obj2; }]; ////上个排序好像不起作用,应采用下面这个 [oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 localizedStandardCompare: obj2];}]; //对数组2排序。 [newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){ return obj1 > obj2; }]; ////上个排序好像不起作用,应采用下面这个 [newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 localizedStandardCompare: obj2];}]; if (newArr.count == oldArr.count) { bol = true; for (int16_t 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(@"两个数组的内容不相同!"); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!