Subarray with range

后端 未结 3 935
南笙
南笙 2021-01-26 11:31

Im trying to split an array of objects into smaller arrays containing 32 objects. With the remaining about being put into the array at the end.

This is the code I\'m usi

3条回答
  •  自闭症患者
    2021-01-26 11:52

    2nd parameter of NSMakeRange is length range to create, not the last index in it. So you need to change your code accordingly (simplifying it a bit):

    NSUInteger count = sharedManager.inventoryArray2.count;
    NSMutableArray *arrayOfArrays = [NSMutableArray array];
    NSUInteger from = 0;
    while (from < count) {
        NSRange range = NSMakeRange(from, MIN(32, count-from));
        NSArray *smallArray = [sharedManager.inventoryArray2 subarrayWithRange:range];
        [arrayOfArrays addObject:smallArray];
    
        from += 32;
    }
    

提交回复
热议问题