0

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.

1
  • You are modifying the same object which is inner array over and over and storing it in outer array instead create new inner array every time when limit which is 5 is exhausted. Commented Jan 3, 2015 at 13:20

3 Answers 3

2

You were on the right track with the first approach, which is simplest.

However, there were a few problems:

i was being set to 0 each time. It needs to be declared outside.

NSMutableArray *outer = [[NSMutableArray alloc] init];
NSMutableArray *inner = nil;
int i = 0;

for (id object in array)
{
    if (inner = nil) {
        inner = [[NSMutableArray alloc] init];
    } 
    [inner addObject:object];
    i ++;
    if (i % 5 == 0) {
        [outer addObject:inner];
        inner = nil;
        i = 0;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
- (NSArray *)Addarraydata : (NSMutableArray *) inputArray{

    NSMutableArray * arrNewArray = [NSMutableArray array];

    for(int i = 0; i < [inputArray count]; i = i + 5){
        if((i + 5) < [inputArray count]){
           [arrNewArray addObject:[inputArray subarrayWithRange:NSMakeRange(i, 5)]];
        }else{
            [arrNewArray addObject:[inputArray subarrayWithRange:NSMakeRange(i, ([inputArray count] - i))]];
        }
    }

    NSLog(@"arrNewArray = %@",arrNewArray);

    return arrNewArray;
}

Comments

0
NSMutableArray *outer = [[NSMutableArray alloc] init];

for (int i = 0; i < [array count] / 5; i++) {
   [outer addObject:[array subarrayWithRange:NSMakeRange(i*5, 5)]];
}

And if array can be a non multiple of 5 and you want to get the rest, you can add this after the for loop :

NSUInteger rest = [array count] % 5;
if (rest != 0) {
   [outer addObject:[array subarrayWithRange:NSMakeRange([array count] / 5 * 5, rest)]];
}

1 Comment

Your for loop is invalid and will never terminate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.