从NSMutablearray获取数据?

时间:2011-09-09 07:37:20

标签: objective-c arrays nsmutablearray

我创建了一个名为numbers的可变数组,他以随机顺序保存了20个不同的数字。 正如我从调试中看到的那样,数据存储在内存中并自动释放。

生成数组后 我必须保存数组中的数据供以后使用,我可以找到一种方法,我需要能够读取数组中的数字,如何做到这一点? 也许是全局的NSMutable数组?

代码:

我用来生成数组并加扰它的代码是:

/*
  Use scrambleArray to scramble any NSMutableArray into random order.
  This method is faster than using a sort with a randomizing compare 
  function, since it scrambles the array
  into random order in a single pass through the array
*/

- (void) scrambleArray: (NSMutableArray*) theArray;
{
  int index, swapIndex;
  int size = (int)[theArray count];
  for (index = 0; index<size; index++)
  {
    swapIndex = arc4random() %  size;
    if (swapIndex != index)
    {
      [theArray exchangeObjectAtIndex: index withObjectAtIndex: swapIndex];
    }
  }

}

/*
 randomArrayOfSize: Create and return a NSMutableArray of NSNumbers, 
 scrambled into random order.
 This method returns an autoreleased array. If you want to keep
 it, save it to a retained property.
*/

-(NSMutableArray*) randomArrayOfSize: (NSInteger) size;
{
  NSMutableArray* result = [NSMutableArray arrayWithCapacity:size];
  int index;

  for (index = 0; index<size; index++)
    [result addObject: [NSNumber numberWithInt: index]];

  [self scrambleArray: result];
  currentIndex = 0; //This is an instance variable. 
  return result;
}

- (void) testRandomArray
{
  NSInteger size = 20;
  int index;
  NSInteger randomValue;
  NSMutableArray* randomArray = [self randomArrayOfSize: size];
  for (index = 0; index< size; index++)
  {

    randomValue = [[randomArray objectAtIndex: currentIndex] intValue];
    NSLog(@"Random value[%d] = %ld", index, randomValue);
    currentIndex++;
    if (currentIndex >= size)
    {
      NSLog(@"At end of array. Scrambling the array again.");
      [self scrambleArray: randomArray];
    }
  }
}

现在我希望能够从其他方法获取randomArray中的数据。 谢谢, 施洛米

1 个答案:

答案 0 :(得分:0)

您可以在课堂上使用属性。例如

@interface myClass : NSObject {

NSMutableArray * randomArray_;
}

@property (retain) NSMutableArray * randomArray;

- (void) scrambleArray: (NSMutableArray*) theArray;
- (NSMutableArray*) randomArrayOfSize: (NSInteger) size;
- (void) myMethodToDoAbilityToGetTheDataInRandomArrayFromMyOtherMethods;
- (void) myOtherMethod;

@end

@implementation myClass

@synthesize randomArray = randomArray_;

- (void) myMethodToDoAbilityToGetTheDataInRandomArrayFromMyOtherMethods {

    NSInteger size = 20;
    self.randomArray = [self randomArrayOfSize: size];

}

- (void) myOtherMethod {//See example of using the property in this method

    [self myMethodToDoAbilityToGetTheDataInRandomArrayFromMyOtherMethods];

    NSInteger size = [self.randomArray count];

    int index;

    NSInteger randomValue;

    for (index = 0; index< size; index++)
    {

         randomValue = [[self.randomArray objectAtIndex: currentIndex] intValue];//use self.randomArray!!!

         NSLog(@"Random value[%d] = %ld", index, randomValue);

         currentIndex++;

    if (currentIndex >= size)
    {

        NSLog(@"At end of array. Scrambling the array again.");

        [self scrambleArray: self.randomArray];//use self.randomArray!!!

    }

 }

}

- (void)dealloc {

    self.randomArray = nil;
    [super dealloc];
}

@end