Ios 有射程的子阵

Ios 有射程的子阵,ios,objective-c,Ios,Objective C,我正在尝试将一个对象数组拆分为包含32个对象的较小数组。剩下的大约在最后放入数组 这是我正在使用的代码 int a = sharedManager.inventoryArray2.count; float b = a / 33; int c = ceilf(b); NSMutableArray *arrayOfArrays = [NSMutableArray array]; int from = 0; int to = 31; for (int e = 0; e <= c; e++)

我正在尝试将一个对象数组拆分为包含32个对象的较小数组。剩下的大约在最后放入数组

这是我正在使用的代码

int a = sharedManager.inventoryArray2.count;
float b = a / 33;
int c = ceilf(b);

NSMutableArray *arrayOfArrays = [NSMutableArray array];
int from = 0;
int to = 31;

for (int e = 0; e <= c; e++) {

    if (sharedManager.inventoryArray2.count < to) {

       NSArray *smallArray = [sharedManager.inventoryArray2 subarrayWithRange:NSMakeRange(from, sharedManager.inventoryArray2.count)]; 
        [arrayOfArrays addObject:smallArray];
    }
    else {
       NSArray *smallArray = [sharedManager.inventoryArray2 subarrayWithRange:NSMakeRange(from, to)];
       from = from + (31+1);
       to = from + 31;
       [arrayOfArrays addObject:smallArray];
    } 
}
我不明白,32-63的范围在0-83的范围内

有什么建议吗

谢谢。
Paul.

NSRange表示起点和从该点开始选择的条目数。。因此,它实际上意味着“起点32,从该点开始选择63个项目”,这将超过您的83个条目(32*+*63)

不,实际上范围不是这样工作的 NSRange{32,63}=>表示从索引32中获取63个元素

以下是文件:

NSRange

A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.

typedef struct _NSRange {
  NSUInteger location;
  NSUInteger length;
 } NSRange;

 location
The start index (0 is the first, as in C arrays). For type compatibility with the rest of the system, LONG_MAX is the maximum value you should use for location.

 length
The number of items in the range (can be 0). For type compatibility with the rest of the system, LONG_MAX is the maximum value you should use for length.

NSMakeRange的第二个参数是要创建的长度范围,而不是其中的最后一个索引。因此,您需要相应地更改代码(稍微简化):

nsuiger count=sharedManager.inventoryArray2.count;
NSMutableArray*arrayOfArrays=[NSMutableArray];
n整数from=0;
while(从<计数){
NSRange range=NSMakeRange(起始,最小(32,计数起始));
NSArray*smallArray=[sharedManager.inventoryArray2 SubrayWithRange:range];
[arrayOfArrays添加对象:smallArray];
from+=32;
}

aha,谢谢。也许我应该更仔细地阅读文档。
NSRange

A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.

typedef struct _NSRange {
  NSUInteger location;
  NSUInteger length;
 } NSRange;

 location
The start index (0 is the first, as in C arrays). For type compatibility with the rest of the system, LONG_MAX is the maximum value you should use for location.

 length
The number of items in the range (can be 0). For type compatibility with the rest of the system, LONG_MAX is the maximum value you should use for length.
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;
}