Zaraki
Zaraki

Reputation: 3730

Copy few elements of array to another

i have two arrays say arrOne and arrTwo. now arrOne is having 27 elements and arrTwo is vacant. On click of a button i want to copy first 10 elements of arrOne to arrTwo, then on second click, i want to add another 10 elements and the rest 7 on another click. example with code would be really helpful,,.. thnx O_o

Upvotes: 0

Views: 682

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40030

Here is how to do it:

    //
    // Filling first array with 20 elements
    //
    NSMutableArray* arrOne = [[NSMutableArray alloc] initWithObjects:nil];
    NSMutableArray* arrTwo = [[NSMutableArray alloc] initWithObjects:nil];

    for (int i=1; i<27; i++) {
        [arrOne addObject:[NSNumber numberWithInt:i]];
    }
    //
    // Adding 10 elements starting from initialPosition to second array
    //
    NSLog(@"arrOne: %@", [arrOne componentsJoinedByString:@", "]);
    int initialPosition = 0; // Just change the initial, starting position to 10, 20, 27 and so on..
    [arrTwo addObjectsFromArray:[arrOne objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(initialPosition, 10)]]];
    NSLog(@"arrTwo: %@", [arrTwo componentsJoinedByString:@", "]);
    [arrOne release];
    [arrTwo release];

Upvotes: 1

Related Questions