开发者

Copy first 10 objects of NSMutableArray to another NSMutable Array

开发者 https://www.devze.com 2023-04-05 14:36 出处:网络
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.

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 addO开发者_StackOverflowbject:@"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 !!!!!


NSArray *tempArray = [arrayAllAnimalImages subarrayWithRange:NSMakeRange(0, 10)];
[arrayLevelImages addObjectsFromArray:tempArray];
[arrayLevelImages addObjectsFromArray:tempArray];


NSArray *arrayLevelImages = [[arrayAllAnimalImages 
                              subarrayWithRange:NSMakeRange(0, 10)] mutableCopy];
0

精彩评论

暂无评论...
验证码 换一张
取 消