如何知道新元素是否添加到 NSArray
How to know if new element added to NSArray
我有一个巨大的array=["1","2","3","4","5","6"]
,随着时间的推移。
例如:array=["1","2","3","4","5","6","10","11"]
。
如何知道两个元素的相加是海量的?在示例中,添加的新元素为 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
)
所以基本上你只需要删除旧元素并显示新元素。
你可以用NSMutableArray
的removeOjbectsInArray
方法试试
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];
我有一个巨大的array=["1","2","3","4","5","6"]
,随着时间的推移。
例如:array=["1","2","3","4","5","6","10","11"]
。
如何知道两个元素的相加是海量的?在示例中,添加的新元素为 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
)
所以基本上你只需要删除旧元素并显示新元素。
你可以用NSMutableArray
removeOjbectsInArray
方法试试
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];