Skip to content

Instantly share code, notes, and snippets.

@CodaFi
Last active December 15, 2015 04:19
Show Gist options
  • Save CodaFi/5201011 to your computer and use it in GitHub Desktop.
Save CodaFi/5201011 to your computer and use it in GitHub Desktop.
Divide an array of Obj-C objects into an arbitrary amount of smaller arrays.
void DMArrayDivide(NSArray **slices, NSArray *inArray, NSUInteger parts) {
if (parts == 0 || parts == 1) {
*slices = inArray;
return;
}
NSMutableArray *arrays = [NSMutableArray arrayWithCapacity:parts];
for (int i = 0; i < parts; i++) {
arrays[i] = @[].mutableCopy;
}
long int idx = 0;
for (id object in inArray) {
[arrays[idx] addObject:object];
idx = (idx + 1) % parts;
}
*slices = arrays;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment