我有2个数组,
我想从另一个中删除一个。
我不能使用 removeObject: 因为指针不同。
我正在根据它们的属性删除对象(xa == ya)
如何根据对象的属性从另一个数组中删除对象?
谢谢,
试试这个
NSMutableArray *arrFirst = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"topic_id",@"abc",@"topic_name",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"2",@"topic_id",@"opq",@"topic_name",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"3",@"topic_id",@"xyz",@"topic_name",nil], nil];
NSArray *arrSec = [NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:@"2",@"topic_id",@"opq",@"topic_name",nil]];
NSArray *temp = [arrFirst filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF IN %@",arrSec]];
[arrFirst removeObjectsInArray:temp];
NSLog(@"%@",arrFirst);
试试这个例子
for(int i = 0; i< firstArray.count ; i++){
NSString *first = [firstArray objectAtIndex:i];
for(int j = 0; j< secondArray.count; j++){
NSString *second = [econdArray objectAtIndex:j];
if([first isEqualToString:second]){
/// Do your methods
}
}
}
单个 for 循环就足够了,因为另一个想法是通过子类化或创建自己的类别来覆盖 removeobject 方法。
BOOL GotOne = NO;
for(int i =0; i < mutableArray.count; i++)
{
NSArray *temp = [mutableArray objectAtIndex:i];
for(int j = 0; j < temp.count; j++)
{
//Do what u want with temp or check any condition
if(success)
{
GotOne = YES;
break;
}
}
if(GotOne)
{
[mutableArray removeObjectAtIndex:i];
GotOne = NO;
}
}