我有一個龐大的array=["1","2","3","4","5","6"]
,隨着時間的推移。例如:array=["1","2","3","4","5","6","10","11"]
。如何知道是否添加新元素NSArray
如何知道兩個元素添加到大量?在本例中,添加了新的元素是10和11
我有一個龐大的array=["1","2","3","4","5","6"]
,隨着時間的推移。例如:array=["1","2","3","4","5","6","10","11"]
。如何知道是否添加新元素NSArray
如何知道兩個元素添加到大量?在本例中,添加了新的元素是10和11
試試這個,
NSArray *array1 = @[@"1",@"2",@"3",@"4",@"5",@"6"];
NSArray *array2 = @[@"1",@"2",@"3",@"4",@"5",@"6",@"10",@"11"];
NSMutableArray *addedArray = [NSMutableArray array];
for(int i = 0; i < [array2 count]; i++)
{
if (![array1 containsObject:[array2 objectAtIndex:i]]) {
[addedArray addObject:[array2 objectAtIndex:i]];
}
}
NSLog(@"New elements is added: %@", addedArray);
輸出:
New elements is added:
(
10,
11
)
'indexesOfObjectsPassingTest:'更快。 – Willeke
所以基本上你只需要刪除舊的元素,並顯示新元素。
您可以通過使用NSMutableArray
i.e.
NSArray *oldArray = @[@"1",@"2",@"3",@"4",@"5",@"6"];
NSArray *newArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"10",@"11"];
NSMutableArray *result = [newArray mutableCopy]; // make sure you make it a mutable copy
// remove the existing elements
[result removeObjectsInArray:oldArray];
NSLog(@"remaining entry: %@",result);
@[10,11];
這真的很難明白你問什麼的
removeOjbectsInArray
方法試試。 – marmeladze