Search code examples
iosobjective-cnsmutablearraynsarray

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
)

Solution

  • 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.