0

我有一个带有动物图像的数组,另一个带有相应动物声音的数组,最后一个带有动物名称的数组。例如:

图像数组:

0:[UIImage imageNamed:@"Elephant.png"]
1:[UIImage imageNamed:@"Dog.png"]
2:[UIImage imageNamed:@"Cat.png"]

声音数组:

0:大象
.mp3 1:狗.mp3
2:猫.mp3

单词数组:

0:大象
1:狗
2:猫

它们是同步的,并与同一个计数器一起工作。我想开始将它们随机排列在新的 3 个数组中,但我希望相同的动物细节(图像或声音或单词)在所有 3 个数组中都相同。例如,新数组应如下所示:

图像数组:

0:[UIImage imageNamed:@"Cat.png"]
1:[UIImage imageNamed:@"Dog.png"]
2:[UIImage imageNamed:@"Elephant.png"]

声音数组:

0:猫.mp3
1:狗.mp3
2:大象.mp3

单词数组:

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];
}

它不工作。为什么会这样,我怎样才能以比我在这里做的更有效的方式做到这一点?

4

3 回答 3

3

这里最好的做法是创建一个类,封装一个单词、声音和图片。

动物.h

@interface Animal : NSObject
{
}

@property (nonatomic, retain) UIImage *picture;
@property (nonatomic, copy) NSString *word;
@property (nonatomic, copy) NSString *soundFile;

+ animalWithPicture:(UIImage *) aPicture word:(NSString *) aWord soundFile:(NSString *) aSoundFile;
- initWithPicture:(UIImage *) aPicture word:(NSString *) aWord soundFile:(NSString *) aSoundFile;

@end

动物.m

#import "Animal.h"

@implementation Animal
@synthesize picture = _picture;
@synthesize word = _word;
@synthesize soundFile = _soundFile;

+ animalWithPicture:(UIImage *) aPicture word:(NSString *) aWord soundFile:(NSString *) aSoundFile
{
    return [[[self alloc] initWithPicture:aPicture word:aWord soundFile:aSoundFile] autorelease];
}

- initWithPicture:(UIImage *) aPicture word:(NSString *) aWord soundFile:(NSString *) aSoundFile
{
    self = [super init];
    if (!self) return nil;
    
    // WARNING, make sure you understand the side effects of using
    // the property accessor methods during "init" and "dealloc".
    // In this case, there should be no issue.
    
    self.picture = aPicture;
    self.word = aWord;
    self.soundFile = aSoundFile;
    
    return self;
}

- (void) dealloc
{
    self.picture = nil;
    self.word = nil;
    self.soundFile = nil;
    [super dealloc];
}

@end

然后,您可以简单地使用一个数组来包含每个动物对象。还可以考虑将您的动物信息存储在 plist 文件中,而不是像我在这里所做的那样对值进行硬编码:

NSMutableArray *animals = [NSMutableArray array];

[animals addObject:[Animal animalWithPicture:[UIImage imageNamed:@"Square_Bee.png"] word:@"Bee" soundFile:@"Bee.mp3"]];
// ... etc ...
[animals addObject:[Animal animalWithPicture:[UIImage imageNamed:@"Square_Sheep.png"] word:@"Sheep" soundFile:@"Sheep.mp3"]];

有关为什么在 and 中使用访问器方法的信息initdealloc请参阅此答案

于 2012-05-14T20:44:55.743 回答
1
  1. 我不会直接将图像数据存储在数组中。相反,您可以存储图像文件名。
  2. 当您从数组中删除对象时,它会重新索引。

您可以使用 NSDictionary 来存储图像文件名、声音文件名和单词组合集。

示例代码:

#define kImageFileName   @"ImageFileName"
#define kSoundFileName     @"SoundFileName"
#define kWord           @"Word" 


NSDictionary *animalRecord = [[NSDictionary alloc]initWithObjectsAndKeys:
                              @"Square_Bee.png", kImageFileName,
                              @"bee.mp3", kSoundFileName,
                              @"Bee", kWord,                              
                              nil];

NSMutableArray *myArray = [[NSMutableArray alloc]init];
[myArray addObject:animalRecord];
// you would need to create some kind of loop and create animalRecord and add it to your myArray for all your picture/sound/word comnination sets.


//To retreive each record
int i = 0; // for example
NSString *imageFileName = [[myArray objectAtIndex:i] objectForKey:kImageFileName];  //with the filename, you can can the image whenever you need
NSString *soundFileName = [[myArray objectAtIndex:i] objectForKey:kSoundFileName];
NSString *word  = [[myArray objectAtIndex:i] objectForKey:kWord];

//to rearrange myArray's objects in random order, try this


 // This piece of code is from this [SO][1]
NSUInteger count = myArray.count;
for (NSUInteger i = 0; i < count; ++i) 
{
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [myArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}

链接到数组重排SO

于 2012-05-14T20:55:55.803 回答
1

另一种方法是创建一个新对象,例如“动物”。Animal 对象将具有三个属性:图片、声音、名称。

然后,您可以使用单个数组并将动物对象添加到数组中。然后遍历数组。

于 2012-05-14T20:38:29.460 回答