I am a newbie to Objective-C and iOS.
I have a NSMutableArray with 180 objects.They are all sorted.
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:180];
for (int i = 0; i < 180; i ++) {
NSString *string = [NSString stringWithFormat:@"%d",i+1];
[array addObject:string];
}
Now, I want to put them into a NSArray, which this NSArray is a five-objects Array.(I mean the array contains 5 object each). And I had come up with two method to do so.
Method 1
NSMutableArray *outer = [[NSMutableArray alloc] init];
NSMutableArray *inner = [[NSMutableArray alloc] initWithCapacity:5];
for (id object in array)
{
int i = 0;
[inner addObject:object];
if (i % 5 == 0) {
[outer addObject:inner];
}
i ++;
}
NSLog(@"%@",outer);
Method 2
NSMutableArray *outer = [[NSMutableArray alloc] init];
NSMutableArray *inner = [[NSMutableArray alloc] initWithCapacity:5];
for (int i = 0; i < 180; i ++)
{
if (i % 5 == 0 && i != 0) {
[outer addObjectsFromArray:inner];
[inner removeAllObjects];
[inner addObject:array[i]];
}else{
[inner addObject:array[i]];
}
}
NSLog(@"%@",outer);
How can I do that? Thanks in advance.