22

I have an array like this:

array: (
    (
        "http://aaa/product/8_1371121323.png",
        "http://aaa/product/14_1371123271.png"
    ),
    (
        "http://aaa/product/9_1371121377.png"
    )
)

and I have to create another array from that one like this

array: (
    "http://aaa/product/8_1371121323.png",
    "http://aaa/product/14_1371123271.png",
    "http://aaa/product/9_1371121377.png"
)

How can I do that? Is it possible to combine all the objects and separate them using some string?

Monolo
  • 18,205
  • 17
  • 69
  • 103
user23790
  • 563
  • 3
  • 21

4 Answers4

114

It can be done in a single line if you like key-value coding (KVC). The @unionOfArrays collection operator does exactly what you are looking for.

You may have encountered KVC before in predicates, bindings and similar places, but it can also be called in normal Objective-C code like this:

NSArray *flatArray = [array valueForKeyPath: @"@unionOfArrays.self"];

There are other collection operators in KVC, all prefixed with an @ sign, as discussed here.

Monolo
  • 18,205
  • 17
  • 69
  • 103
  • Very nice! I'm just curious: did you check if that works with more deeply nested arrays? – Martin R Jun 13 '13 at 22:12
  • @MartinR pretty sure you can't get collection operators to work recursively to arbitrary depths, but this one works on multi-dimensional arrays. I posted a self-answered question with the results of a small experiment: http://stackoverflow.com/q/17109942/644348 – Monolo Jun 14 '13 at 13:45
6

Sample Code :

NSMutableArray *mainArray = [[NSMutableArray alloc] init];
for (int i = 0; i < bigArray.count ; i++)
{
     [mainArray addObjectsFromArray:[bigArray objectAtIndex:i]];
}
NSLog(@"mainArray :: %@",mainArray);
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • Maybe esear use NSMutableArray?) And in the end [[NSArray alloc] initWithArray:...] – Gralex Jun 13 '13 at 12:54
  • @Sk0prion: No need to use `NSMutableArray` till the OP wants add something later in the Array. – Bhavin Jun 13 '13 at 12:56
  • 3
    @Vin: If array of arrays is too large, better to use NSMutableArray. If u use NSArray, in each iteration u create new array. It's not good affect to performance. In the end u can translate mutable array to not mutable. [NSArray arrayWithArray:mutableArr] – Gralex Jun 13 '13 at 13:12
  • @Sk0prion: Right Point. I didn't think about it. Thanks for pointing out.Is it ok now ? – Bhavin Jun 13 '13 at 13:17
  • 1
    @Vin: Yes) But if need more performance, u can go throw array like this for(NSArray* smallArr in bigArray){ [mainArray addObjectsFromArray:smallArr]; } ; This code faster and cleaner – Gralex Jun 13 '13 at 13:30
  • @Sk0prion: Can you explain me why it's faster and cleaner ? – Bhavin Jun 13 '13 at 13:32
  • @Vin: I don't know realy implementation of arrays, but I think it uses list structure for implementation of array. Imagine list with 10000 elements. If you need to access 1000 element of array, you need go throw 999 nodes of the list to access to 1000 element. But I not sure, maby it uses tree structure. Anyway you need some time to access some element of array. In my variant it goes successively throw structure, so it's faster. Cleaner- this code easier to read, anderstand – Gralex Jun 13 '13 at 13:44
1

Sample code:

NSArray* arrays = @(@(@"http://aaa/product/8_1371121323.png",@"http://aaa/product/14_1371123271.png"),@(@"http://aaa/product/9_1371121377.png"));
NSMutableArray* flatArray = [NSMutableArray array];
for (NSArray* innerArray in arrays) {
    [flatArray addObjectsFromArray:innerArray];
}

NSLog(@"%@",[flatArray componentsJoinedByString:@","]);
Andrei Shender
  • 2,487
  • 22
  • 15
-1
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[initialArray objectAtIndex:0]];
[arr1 addObjectsFromArray:[initialArray objectAtIndex:1]];

Now arr1 contains all the objects

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161