I have two different NSMutabelArray ArrOne and ArrTwo. Letsay ArrOne = A, B, C and D ArrTwo = C, D, X and Y. So i need to check if the value of ArrTwo is same as ArrOne and remove item from ArrTwo if it is not same as in ArrOne. In this case, i have to remove X and Y from ArrTwo. Please give me an idea.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
NSMutableSet *set = [NSMutableSet setWithArray:arrOne];
[set intersectSet:[NSSet setWithAray:arrTwo];
return [set allObjects];
回答2:
You can do it with indexesOfObjectsPassingTest, like this:
NSMutableArray *a = [@[@"A",@"B",@"C",@"D"] mutableCopy];
NSMutableArray *b = [@[@"C",@"D",@"X",@"Y"] mutableCopy];
NSIndexSet *indxs = [b indexesOfObjectsPassingTest:^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
return ![a containsObject:obj];
}];
[b removeObjectsAtIndexes:indxs];
回答3:
I found a solution and it works
for (int i=0; i< arrTwo.count; i++)
{
if(![arrOne containsObject:[arrTwo objectAtIndex:i]])
{
//do action
NSLog(@"do delete %@",[arrTwo objectAtIndex:i]);
}
}
Thanks!