iOS - Make a copy of a array object into another

2019-05-17 20:27发布

问题:

I have a little problem, and I need help.

I want to loop through a multidimensional array, and every time I find a value for a key ex."name" that is equal to ex. "Hello". I want to copy that array object into another array.

How do I do that?

回答1:

I assume you have a 2-dimensional array and since I don't know how your objects within the array look like I take the id type and the method valueForKey:

 NSMutableArray *tmp = [[NSMutableArray alloc] init];
 for(NSArray *dim1Array in yourMultidimensionalArray)
 {
    for(id obj in dim1Array)
    {
       if([[obj valueForKey:@"name"] isEqualToString:@"Hello"])
       {
          [tmp addObject:dim1Array];
          break; // I assume you only want to add it once
       }
    }
 }


回答2:

This is how you would do it:

NSArray* newArray = [NSArray arrayWithArray:oldArray]