具有范围的子阵列 [英] Subarray with range

查看:100
本文介绍了具有范围的子阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个对象数组拆分为包含32个对象的较小数组。剩下的就是最后放入数组。

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.

这是我正在使用的代码

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];
    } 
}

我收到以下错误。

'NSRangeException', reason: '*** -[NSArray subarrayWithRange:]: range {32, 63} extends beyond bounds [0 .. 83]'   

我不明白,32-63的范围是在界限内0-83。

I don't get it, the range of 32-63 is in the bounds of 0-83.

有什么建议吗?

谢谢。
保罗。

Thanks. Paul.

推荐答案

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

A NSRange indicates the starting point and the number of entries to select from that point on.. So it actually means "Starting point 32, select 63 items from that point on", which will exceed your 83 entries (32*+*63)

这篇关于具有范围的子阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆