Flatten an NSArray

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.

Leave a Comment