如何知道NSArray中是否添加了新元素

时间:2017-02-02 08:19:53

标签: objective-c nsarray

随着时间的推移,我有一个巨大的array=["1","2","3","4","5","6"]

例如:它将是array=["1","2","3","4","5","6","10","11"]

如何知道两个元素增加了大量?在示例中,添加的新元素是10和11。

2 个答案:

答案 0 :(得分:2)

所以基本上你只需要删除旧元素并显示新元素。

您可以尝试使用removeOjbectsInArray

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];

答案 1 :(得分:0)

试试这个,

<span class="js-display-url">product/poss/newslanges</span>

<强>输出:

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);