IOS how to get subarray from array using subarraywithRange

I have a problem using subArrayWithRange.

Basically, what I want to do is to make a subarray of 50 elements or less from mainArray, for example, if mainArray has 70 elements, I want sortedArray to have an array of the first 50 elements in the first index and another array of 20 items in the latest sortedArray index

I hope that I understand what I want to do.

Anyway, my code

for (int i=0; i<=ceilLoopCount; i++) {
    [sortedArray insertObject:[testArray subarrayWithRange:NSMakeRange(0,50)] atIndex:i]; 
} 

and the problem I am facing is that I only get 50 elements in the whole array

Please help, Pondd

+3
source share
2 answers
NSUInteger size = 50;

for (NSUInteger i = 0; i * size < [testArray count]; i++) {
  NSUInteger start = i * size;
  NSRange range = NSMakeRange(start, MIN([testArray count] - start, size));
  [sortedArray addObject:[testArray subarrayWithRange:range]];
}
+14
source
            NSMutableArray *arrayOfArrays = [NSMutableArray array];
            int batchSize = 30;

            for(int j = 0; j < [stuff count]; j += batchSize) {

                NSArray *subarray = [stuff subarrayWithRange:NSMakeRange(j, MIN(batchSize, [stuff count] - j))];
                [arrayOfArrays addObject:subarray];
            }
0
source

All Articles