Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split an NSMutableArray into other NSMutableArrays

I have an NSMutableArray with 50 entries - is there any easy way to split this into 5 NSMutableArrays each with 10 entries each.

like image 438
Dominic Avatar asked Dec 06 '25 07:12

Dominic


2 Answers

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.

like image 197
Andrew Hoos Avatar answered Dec 07 '25 20:12

Andrew Hoos


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.

like image 23
CodaFi Avatar answered Dec 07 '25 20:12

CodaFi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!