【发布时间】:2012-05-14 19:54:08
【问题描述】:
我有一个包含动物图像的数组,另一个包含相应动物声音的数组,最后一个包含动物名称的数组。例如:
imagesArray:
0:[UIImage imageNamed:@"Elephant.png"]
1:[UIImage imageNamed:@"Dog.png"]
2:[UIImage imageNamed:@"Cat.png"]
soundsArray:
0: 大象.mp3
1:狗.mp3
2:猫.mp3
wordsArray:
0:大象
1:狗
2:猫
它们是同步的,并与同一个计数器一起工作。我想开始将它们随机排列在新的 3 个数组中,但我希望相同的动物细节(图像或声音或单词)在所有 3 个数组中都相同。例如,新数组应如下所示:
imagesArray:
0:[UIImage imageNamed:@"Cat.png"]
1:[UIImage imageNamed:@"Dog.png"]
2:[UIImage imageNamed:@"Elephant.png"]
soundsArray:
0: Cat.mp3
1:狗.mp3
2:大象.mp3
wordsArray:
0:猫
1:狗
2:大象
我把这段代码写到确实加载的视图中:
theNewAnimalsPicture = [NSArray arrayWithObjects:[UIImage imageNamed:@"Square_Bee.png"],[UIImage imageNamed:@"Square_Bird.png"],[UIImage imageNamed:@"Square_Cat.png"],[UIImage imageNamed:@"Square_Chicken.png"],[UIImage imageNamed:@"Square_Cow.png"],[UIImage imageNamed:@"Square_Dog.png"],[UIImage imageNamed:@"Square_Elephant.png"],[UIImage imageNamed:@"Square_Frog.png"],[UIImage imageNamed:@"Square_Horse.png"],[UIImage imageNamed:@"Square_Lion.png"],[UIImage imageNamed:@"Square_Monkey.png"],[UIImage imageNamed:@"Square_Sheep.png"], nil];
theNewAnimalsSound = [NSArray arrayWithObjects:@"Bee.mp3",@"Bird.mp3",@"Miao.mp3",@"Chicken.mp3",@"Cow.mp3",@"Waff.mp3",@"Elephant2.mp3",@"Frog.mp3",@"Horse.mp3",@"LionSound.mp3",@"Monkey_Sound.mp3",@"Sheep.mp3", nil];
theNewAnimalsWord = [NSArray arrayWithObjects:@"Bee",@"Bird",@"Cat",@"Chicken",@"Cow",@"Dog",@"Elephant",@"Frog",@"Horse",@"Lion",@"Monkey",@"Sheep", nil];
for (int i =0;i<[theNewAnimalsPicture count];i++)
{
int number = arc4random() % [theNewAnimalsPicture count];
[PicturesAnimalArray addObject:[theNewAnimalsPicture objectAtIndex:number]];
[SoundsAnimalArray addObject:[theNewAnimalsSound objectAtIndex:number]];
[wordAnimalArray addObject:[theNewAnimalsWord objectAtIndex:number]];
[theNewAnimalsPicture removeObjectAtIndex:number];
[theNewAnimalsSound removeObjectAtIndex:number];
[theNewAnimalsWord removeObjectAtIndex:number];
}
它不工作。为什么会这样,我怎样才能以比我在这里更有效的方式做到这一点?
【问题讨论】:
-
我认为应该可以,你能提供你得到的错误,或者 3 个新数组的输出吗?
-
这是一个更好地理解数组的练习,还是您需要它来为您的项目工作?如果是后者,我建议制作一个 Animal 对象,它具有图片声音和单词作为属性,然后只有一个数组!
-
我确实需要这个,但我刚完成学习。是的,创建一个动物对象当然更好。
标签: objective-c nsmutablearray shuffle