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(@"兩個數組的內容不相同!"); }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!