简体   繁体   中英

remove object from NSMutableArray that contains NSArray

I have an NSMutableArray of NSArray's inside it. This is my current structure:

    Paused index (
       (
          0,
          3579
       ),
       (
          1,
          3538
       )
    )

how to search through the array and remove the object 3538? My expected result is:

    Paused index (
       (
          0,
          3579
       )
    )

How can I get the index of the outer array? I can already do the search but how can I remove the object in that index?

This is a very straight forward way.

    NSArray *firstArray =  [NSArray arrayWithObjects:[NSNumber numberWithInt:100],[NSNumber numberWithInt:1000], nil];
NSArray *secondArray =  [NSArray arrayWithObjects:[NSNumber numberWithInt:100],[NSNumber numberWithInt:5000], nil];

NSMutableArray *arrayWithArray = [NSMutableArray arrayWithObjects:firstArray,secondArray,nil];
int i = 0;

for (NSArray *innerArray in arrayWithArray) 
{
    if ([innerArray containsObject:[NSNumber numberWithInt:5000]])
    {
        break;
    }
    i++;

}
NSLog(@"Index of object %d",i);
[arrayWithArray removeObjectAtIndex:i];

Some other intelligent way should be there.

EDIT: I think this is little better.

int i = [arrayWithArray indexOfObjectPassingTest:^BOOL(id element,NSUInteger idx,BOOL *stop)
    {
       return [(NSArray *)element containsObject:[NSNumber numberWithInt:5000]];
    }];

    NSLog(@"Index of object %d",i);
    if (i >= 0 && i < [arrayWithArray count]) 
    {
         [arrayWithArray removeObjectAtIndex:i];
    }

As you have this array

Array_Paused_index (

   (
      0,
      3579
   ),
   (
      1,
      3538
   )

)

Take one more Array

Array = [Array_Paused_index objectAtIndex: 0];

Result will be

Array

(

 (

   0,

   3579

 )

)

OR SECOND METHOD :

[Array_Paused_index removeObjectAtIndex:1];

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