1. 程式人生 > >ios開發之--比較兩個數組裏面的值是否相同

ios開發之--比較兩個數組裏面的值是否相同

相同 obj sco break nsa iso img ios開發 mut

比較兩個數組裏面的內容是否相同,代碼如下:

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(@"兩個數組的內容不相同!"); }

打印如下:

技術分享圖片

然後再在合適的場景下進行操作。

ios開發之--比較兩個數組裏面的值是否相同