如何在多个相应的NSMutableArrays中随机重新排列数据?

时间:2012-05-14 19:54:08

标签: objective-c nsmutablearray shuffle

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

imagesArray:

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

soundsArray:

0:Elephant.mp3
1:Dog.mp3
2:Cat.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:Dog.mp3
2:Elephant.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 个答案:

答案 0 :(得分:3)

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

Animal.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

Animal.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"]];

有关在initdealloc中使用访问者方法的原因的信息,请参阅this answer

答案 1 :(得分:1)

你可以做的另一种方法是创建一个新对象,比如'Animal'。具有三个属性的Animal对象:图片,声音,名称。

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

答案 2 :(得分:1)

  1. 我不会直接将图像数据存储在数组中。相反,您可以存储图像文件名。
  2. 从数组中删除对象时,会重新编制索引。
  3. 您可以使用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