简体   繁体   中英

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

I have 2 arrays,

I want to remove the one from the other.

I can't use removeObject: because the pointers are different.

I'm removing objects based on their properties (xa == ya)

How do I remove the object based on its properties from the other array?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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