Created by Jamz Tang at 10 December 2011

Splitting an array to several components

Say you’d like to split an array into several arrays each with a specific number of components.

// Say your originalArray is [@"A", @"B", @"C", @"D"]
NSArray *originalArray = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", nil];

// And we now want the array to be split into two arrays with two per segment
// like this [[@"A", @"B"], [@"C", @"D"]]
NSArray *newArray = [NSArray splitArray:originalArray componentsPerSegment:1];

And here’s the code snippet on how you can do it.


Clone this repository:
git clone git:// NSArray-JTArraySplit

| More...

If you think this is useful, share this article with your friends :)

blog comments powered by Disqus