Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy first 10 objects of NSMutableArray to another NSMutable Array

I have a NSMutableArray of 35 items and I want to copy the first 10 items to another array.

After adding 10 items to my array I want to add the same 10 items again.

Please suggest how could I do it.

Code :

i have initialize the array having all images in this way

    self.arrayAllAnimalImages = [[NSMutableArray alloc] init];

[self.arrayAllAnimalImages addObject:@"bagheera.jpg"];
[self.arrayAllAnimalImages addObject:@"baloo.jpg"];
[self.arrayAllAnimalImages addObject:@"bear.jpg"];
[self.arrayAllAnimalImages addObject:@"bird.jpg"];
[self.arrayAllAnimalImages addObject:@"camel.jpg"];
[self.arrayAllAnimalImages addObject:@"cock.jpg"];
[self.arrayAllAnimalImages addObject:@"deer.jpg"];
[self.arrayAllAnimalImages addObject:@"dolphin.jpg"];
[self.arrayAllAnimalImages addObject:@"donkey.jpg"];
[self.arrayAllAnimalImages addObject:@"elephant.jpg"];
[self.arrayAllAnimalImages addObject:@"fish.jpg"];
[self.arrayAllAnimalImages addObject:@"flamigo.jpg"];
[self.arrayAllAnimalImages addObject:@"frog.jpg"];
[self.arrayAllAnimalImages addObject:@"girraf.jpg"];
[self.arrayAllAnimalImages addObject:@"hippo.jpg"];
[self.arrayAllAnimalImages addObject:@"monkey.jpg"];
[self.arrayAllAnimalImages addObject:@"octopus.jpg"];
[self.arrayAllAnimalImages addObject:@"owl.jpg"];
[self.arrayAllAnimalImages addObject:@"panda.jpg"];
[self.arrayAllAnimalImages addObject:@"parrot.jpg"];
[self.arrayAllAnimalImages addObject:@"peacock.jpg"];
[self.arrayAllAnimalImages addObject:@"penguin.jpg"];
[self.arrayAllAnimalImages addObject:@"piglet.jpg"];
[self.arrayAllAnimalImages addObject:@"polarbear.jpg"];
[self.arrayAllAnimalImages addObject:@"pumba.jpg"];
[self.arrayAllAnimalImages addObject:@"rabbit.jpg"];
[self.arrayAllAnimalImages addObject:@"redpanda.jpg"];
[self.arrayAllAnimalImages addObject:@"seahorse.jpg"];
[self.arrayAllAnimalImages addObject:@"simba.jpg"];
[self.arrayAllAnimalImages addObject:@"snake.jpg"];
[self.arrayAllAnimalImages addObject:@"starfish.jpg"];
[self.arrayAllAnimalImages addObject:@"tiger.jpg"];
[self.arrayAllAnimalImages addObject:@"tigger.jpg"];
[self.arrayAllAnimalImages addObject:@"turtle.jpg"];
[self.arrayAllAnimalImages addObject:@"whale.jpg"];

I want 10 items from the above array randomly so Here I have a shuffled array

for (int i = 0; i<[arrayAllAnimalImages count] ;i++)
    {
        NSUInteger randomIndex = arc4random() % [arrayAllAnimalImages  count];
        [arrayAllAnimalImages  exchangeObjectAtIndex:i withObjectAtIndex:randomIndex];  


    }   

Now I want these 10 items to add in an empty array I initialize here :

if (LevelCount ==2)
{
    self.arrayLevelImages= [[NSMutableArray alloc] init];

    if ([arrayLevelImages count] > 0)
    {
        [arrayLevelImages removeAllObjects];
    }

Now here I want to add the first 10 objects of arrayAllAnimalImages.

After adding First 10 items, I want to add the same previous 10 items again to arrayLevelImages.

}   

Please Help !!!!!

like image 501
NSException Avatar asked Sep 19 '11 07:09

NSException


1 Answers

NSArray *tempArray = [arrayAllAnimalImages subarrayWithRange:NSMakeRange(0, 10)];
[arrayLevelImages addObjectsFromArray:tempArray];
[arrayLevelImages addObjectsFromArray:tempArray];
like image 185
adamsiton Avatar answered Nov 19 '22 13:11

adamsiton