I have an NSMutableArray
with n
entries. Is there any easy way to split this into a other NSMutableArray
s each with 25 entries each. Value of n
can be greater than or less than 25.
Asked
Active
Viewed 1,086 times
-1

Amar
- 13,202
- 7
- 53
- 71

sureshJangid
- 145
- 9
-
how it can be possible if array is less then 25 elements? – Retro Nov 14 '13 at 07:35
-
@AashishJoshi If array is less than 25 element .. No need to break. We need to break array if it has count more than 25. – sureshJangid Nov 14 '13 at 07:36
-
possible duplicate of [Split an NSMutableArray into other NSMutableArrays](http://stackoverflow.com/questions/17117339/split-an-nsmutablearray-into-other-nsmutablearrays) and http://stackoverflow.com/questions/1768081/how-to-split-an-nsarray-into-two-equal-pieces/1768119#1768119 – Woodstock Nov 14 '13 at 07:38
-
ok, so follow the folks link, they have searched solution for you :) – Retro Nov 14 '13 at 07:39
1 Answers
6
You can use the below method to split your array
-(NSMutableArray*)splittedArrayFrom:(NSArray*)inputArray divideCount:(int)cnt
{
NSMutableArray *mainArray = [[NSMutableArray alloc]init];
int itemsRemaining = [inputArray count];
for (int i =0;i*cnt<[inputArray count]; i++) {
NSRange range = NSMakeRange(i*cnt, MIN(cnt, itemsRemaining));
NSArray *childArray = [inputArray subarrayWithRange:range];
[mainArray addObject:childArray];
itemsRemaining = itemsRemaining - range.length;
}
return mainArray;
}

manujmv
- 6,450
- 1
- 22
- 35