You can use something like this.
NSMutableArray *rootArray = [NSMutableArray array];
for (NSInteger i = 1; i < [items count]; i+=2) {
id object1 = [items objectAtIndex:i-1];
id object2 = [items objectAtIndex:i];
[rootArray addObject:[NSArray arrayWithObjects:object1, object2, nil]];
}
This will ignore the last object if you have an odd number of objects in your array.
Change, a version that does not ignore the last single object.
NSMutableArray *rootArray = [NSMutableArray array];
for (NSInteger i = 0; i < [items count]; i += 2) {
id object1 = [items objectAtIndex:i];
id object2 = nil;
if (i+1 < [items count]) {
object2 = [items objectAtIndex:i+1];
}
[rootArray addObject:[NSArray arrayWithObjects:object1, object2, nil]];
}
source
share