Dominic
Dominic

Reputation: 351

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.

Upvotes: 3

Views: 1581

Answers (2)

CodaFi
CodaFi

Reputation: 43330

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.

Upvotes: 2

Andrew Hoos
Andrew Hoos

Reputation: 1520

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.

Upvotes: 4

Related Questions