Vjardel
Vjardel

Reputation: 1075

iOS Get all NSArray into one

I've NSArray with NSArray in. I would like to get all NSArray into one.

I think there is a simple function to use, isn't it ?

Here is what I receive :

(
        (
        n0eGi1KJWq,
        KHGeW32548,
    ),
        (
        n0eGi1KJWq
    )
)

I would like to get :

(
        n0eGi1KJWq,
        KHGeW32548,
        n0eGi1KJWq
)

Upvotes: 0

Views: 51

Answers (2)

alexburtnik
alexburtnik

Reputation: 7741

You can also use @unionOfArrays for this purpose:

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

If you need a deeper flattening (for three dimensional array), use it twice:

NSArray *flatArray = [array valueForKeyPath: @"@[email protected]"];

Upvotes: 1

rmaddy
rmaddy

Reputation: 318774

A simply loop can be used to create a new array:

NSArray *mainArray = ... // the array containing the other arrays
NSMutableArray *finalArray = [NSMutableArray array];
for (NSArray *innerArray in mainArray) {
    [finalArray addObjectsFromArray:innerArray];
}

At this point finalArray will have all of the objects from all of the other arrays.

Upvotes: 3

Related Questions