I have an NSMutableArray with 50 entries - is there any easy way to split this into 5 NSMutableArrays each with 10 entries each.
Asked
Active
Viewed 1,570 times
3
-
Yes, there is. It's quite obvious, indeed. – Jun 14 '13 at 21:13
-
It took me 30 seconds to google it and find a solution, http://stackoverflow.com/a/1768119/2315974 – danypata Jun 14 '13 at 21:23
-
possible duplicate of [How to split an NSArray into two equal pieces?](http://stackoverflow.com/questions/1768081/how-to-split-an-nsarray-into-two-equal-pieces) – wattson12 Jun 14 '13 at 22:12
2 Answers
4
Yes To divide the NSMutableArray you can use the NSArray method
- (NSArray *)subarrayWithRange:(NSRange)range
NSRange is a struct that is a start location and number of items. So you would want 0 and 10, 10 and 10, 20 and 10, etc.
Use the following function to make your arrays:
NSRange NSMakeRange (NSUInteger loc, NSUInteger len );
Hope that helps.

Andrew Hoos
- 1,510
- 2
- 15
- 23
2
I wrote a short function to do this a while ago. The gist of it is to loop an array filling an arbitrary array at the loop index modulo the number of subarrays you want until you run out of items.

CodaFi
- 43,043
- 8
- 107
- 153