简体   繁体   English

如何从另一个 NSMutableArray 中删除一个 NSArray 的对象?

[英]How to remove one NSArray's objects from another NSMutableArray?

I have 2 arrays,我有 2 arrays,

I want to remove the one from the other.我想从另一个中删除一个。

I can't use removeObject: because the pointers are different.我不能使用 removeObject: 因为指针不同。

I'm removing objects based on their properties (xa == ya)我正在根据它们的属性删除对象 (xa == ya)

How do I remove the object based on its properties from the other array?如何根据其他数组的属性删除 object?

Thanks,谢谢,

try this试试这个

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

Try This example试试这个例子

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

      }
   }
}

Single for loop is sufficient, as another idea would be to overwrite the removeobject method either by subclassing or making own category.单个 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;
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM