什么是一个简单的方法来打破一个4000多个对象的NSArray到多个数组,每个对象有30个对象? [英] What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?
问题描述
什么是一个简单的方法打破NSArray与4000个对象在它的多个数组,每个有30个对象?
What is an easy way to break an NSArray with 4000 objects in it into multiple arrays with 30 objects each?
现在我有一个NSArray * [stuff count] = 4133。
So right now I have an NSArray *stuff where [stuff count] = 4133.
我想创建一个包含30个对象的数组的新数组。什么是一个好的方法循环,打破*的东西到新的30对象数组,并将它们放在一个更大的数组?
I want to make a new array that holds arrays of 30 objects. What is a good way to loop through, breaking *stuff into new 30-object arrays, and placing them inside of a larger array?
显然,最后一个数组不会有30个(它将有剩余),但我需要正确处理。
Obviously the last array won't have 30 in it (it will have the remainder) but I need to handle that correctly.
有意义吗?
推荐答案
在我的头顶,像(未测试的) :
Off the top of my head, something like (untested):
NSMutableArray *arrayOfArrays = [NSMutableArray array];
int itemsRemaining = [stuff count];
int j = 0;
while(itemsRemaining) {
NSRange range = NSMakeRange(j, MIN(30, itemsRemaining));
NSArray *subarray = [stuff subarrayWithRange:range];
[arrayOfArrays addObject:subarray];
itemsRemaining-=range.length;
j+=range.length;
}
MIN(30,i)
处理最后一个数组,不一定有30个项目。
The MIN(30, i)
takes care of that last array that won't necessarily have 30 items in it.
这篇关于什么是一个简单的方法来打破一个4000多个对象的NSArray到多个数组,每个对象有30个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!