ytpm
ytpm

Reputation: 5150

How can I randomly rearrange data in multiple corresponding NSMutableArrays?

I have an array with images of animals, another array with the corresponding animal's sound and the last one with the animal's name. for example:

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: Elephant
1: Dog
2: Cat

They are sync and work together with the same counter. I want at start to arrange them randomly in new 3 arrays, but I want the same animal details (image or sound or word) to be the same in all 3 arrays. For example, the new arrays should look like this:

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: Cat
1: Dog
2: Elephant

I wrote this code to the view did load:

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

It isn't working. Why is that, and how can I do this in more efficient way than what I did here?

Upvotes: 0

Views: 227

Answers (3)

user523234
user523234

Reputation: 14834

  1. I would not store image data in the array directly. Instead you can store the image file name.
  2. When you remove object from array, it re-indexed.

You can use a NSDictionary to store your image file name, sound file name, and word combination set.

Sample code:

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

Link to array rearrangement SO

Upvotes: 1

dreamlax
dreamlax

Reputation: 95335

The best thing to do here is to create a class that encapsulates one word, sound and picture.

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

Then, you could simply have a single array to contain each animal object. Consider also storing your animal information in a plist file rather than hard-coding the values like I have done here:

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

For information about why using accessor methods in init and dealloc, see this answer.

Upvotes: 3

Joe
Joe

Reputation: 2987

Another way you could do it is Create a new object, something like 'Animal'. The Animal object which would have three properties: picture, sound, name.

You could then use a single array and add the animal object to the array. Then iterate over the array.

Upvotes: 1

Related Questions