我有一个NSArray
包含对象
[ObjA,ObjB,ObjC,ObjD,ObjE ...]
我想像这样创建一个3xN维度数组:
[ObjA,ObjB,ObjC]
[ObjD,ObjE,ObjF]
[...]
我似乎无法绕过我的for循环。
答案 0 :(得分:2)
const NSInteger objsPerGroup = 3;
NSArray *flatArray = @[objA, objB, objC, objD, objE, objF];
NSMutableArray *groupedArray = [NSMutableArray arrayWithCapacity:([flatArray count] / objsPerGroup)];
NSInteger index = 0;
NSMutableArray *currentGroup = nil;
for (id obj in flatArray)
{
if (index % objsPerGroup == 0)
{
currentGroup = [NSMutableArray arrayWithCapacity:objsPerGroup];
[groupedArray addObject:currentGroup];
}
[currentGroup addObject:obj];
++index;
}