【问题标题】:How can I randomly rearrange data in multiple corresponding NSMutableArrays?如何随机重新排列多个相应 NSMutableArrays 中的数据?
【发布时间】: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


【解决方案1】:

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

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

有关为什么在initdealloc 中使用访问器方法的信息,请参阅this answer

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      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

      【讨论】:

      • 将图像存储在数组中没有任何问题。缓存和交换将确保应用程序使用的内存不会超过它必须使用的内存。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多