-1

我有2个数组,

我想从另一个中删除一个。

我不能使用 removeObject: 因为指针不同。

我正在根据它们的属性删除对象(xa == ya)

如何根据对象的属性从另一个数组中删除对象?

谢谢,

4

4 回答 4

4

试试这个

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);
于 2012-04-04T13:15:08.513 回答
0

试试这个例子

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

      }
   }
}
于 2012-04-04T12:44:19.757 回答
0

单个 for 循环就足够了,因为另一个想法是通过子类化或创建自己的类别来覆盖 removeobject 方法。

于 2012-04-04T12:46:31.957 回答
0
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;
      }
}
于 2012-04-04T12:51:42.347 回答