2017-02-02 61 views
0

我有一個龐大的array=["1","2","3","4","5","6"],隨着時間的推移。例如:array=["1","2","3","4","5","6","10","11"]如何知道是否添加新元素NSArray

如何知道兩個元素添加到大量?在本例中,添加了新的元素是10和11

+2

這真的很難明白你問什麼的removeOjbectsInArray方法試試。 – marmeladze

回答

0

試試這個,

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 
) 
+0

'indexesOfObjectsPassingTest:'更快。 – Willeke

2

所以基本上你只需要刪除舊的元素,並顯示新元素。

您可以通過使用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]; 
相關問題