1

I have an array contains array at each index in it.

array is :(
        (
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-19g1.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-20g2.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-20g3.jpg"
    ),
        (
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-49y1.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-50y2.jpg"
    ),
        (
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg"

    )
)

I want to make a single array of that like

  (  
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg", 
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg",                    
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg"
    )

How can I eliminate (),() inside the array and make a single array containing the urls.

2
  • 4
    Those characters aren't in the array -- they're there to help you understand the dump. In this case, you have dump of an array which contains 3 other arrays.
    – Hot Licks
    Jan 16, 2014 at 18:30
  • 1
    @HotLicks, I think OP knows that. He's asking how to make a new flat array from the nested arrays.
    – Carl Norum
    Jan 16, 2014 at 18:31

4 Answers 4

8

You'll need to make a new array:

NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSArray *a in array)
    [newArray addObjectsFromArray:a];
2
  • Nice for a one-deep nested array!
    – zaph
    Jan 16, 2014 at 18:33
  • Ooh, I forgot about addObjectsFromArray. Well done. Simpler than my code.
    – Duncan C
    Jan 16, 2014 at 18:38
6

You can flatten your array using the Key-Value Coding operator "@unionOfArrays":

NSArray *nested = @[@[@"A1", @"A2", @"A3"], @[@"B1", @"B2", @"B3"], @[@"C1", @"C2", @"C3"]];
NSArray *flattened = [nested valueForKeyPath:@"@unionOfArrays.self"];

NSLog(@"nested = %@", nested);
NSLog(@"flattened = %@", flattened);

Output:

nested = (
        (
        A1,
        A2,
        A3
    ),
        (
        B1,
        B2,
        B3
    ),
        (
        C1,
        C2,
        C3
    )
)
flattened = (
    A1,
    A2,
    A3,
    B1,
    B2,
    B3,
    C1,
    C2,
    C3
)
0
3

You need to write code to walk your outer array, copying the contents of the second-level array to a "flat" array. Something like this:

(Edited based on Carl Norum's post to use addObjectsFromArray)

-(NSArray )flattenArray: (NSArray *) sourceArray;
{
  NSMutableArray *result = [[NSMutableArray alloc] init];
  for (NSArray *array sourceArray)
  {
  //Make sure this object is an array of some kind. 
  //(use isKindOFClass to handle different types of array class cluster)
  if ([array isKindOfClass: [NSArray class])
  {
    [result addObjectsFromArray: array];
  }
  else
  {
    NSLog(@"Non-array object %@ found. Adding directly.", array);
    [result addObject: array];
  }
  return [result copy]; //return an immutable copy of the result array
}
4
  • This quietly strips any non-array members from the original array, which is confusing. A crash on unexpected structure is ok, but quietly ignoring it is dangerous. There's also no reason to return a copy. There is nothing wrong with returning the NSMutableArray directly.
    – Rob Napier
    Jan 16, 2014 at 18:41
  • I am having an array where each index of array is having another array.
    – Exception
    Jan 16, 2014 at 18:45
  • @Rob Napier, I could just as easily have decided to add non-array objects to the flattened array. That sort of specific error handling decision depends on the needs of the application. For forum posts I tend to do only the bare minimum that answers the question, and leave a fleshed out method with all the edge case checking as an exercise for the OP.
    – Duncan C
    Jan 16, 2014 at 19:52
  • That's fine, but adding the 'isKindOfClass:' check violates that premise. It's ok to say "this would crash if the assumptions aren't valid." Or even "you should check for errors here" is fine. But going out of your way to make it silently swallow the error is dangerous. You could just remove the entire isKindOfClass: check if you don't care about that condition.
    – Rob Napier
    Jan 16, 2014 at 20:13
2

you'll have to normalise your array loop through the array, then all of it's sub arrays and add them to another array Something like this should be enough to get you started: here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.