开发者

getting data from NSMutable array?

I have created a mutable array called numbers and he holds up 20 diffrent numbers in a random order. as I saw from debugging it, the data stored in the memory and auto dealloc itself.

after I generate the array i must save data in the array for later use and I can find a way do to it, I need to be able to read the numbers in the array , how it can be done? maybe global NSMutable array?

the code:

the code I used to generate the array and scrambled it is:

/*
  Use scrambleArray to scramble any NSMutableArray into random order.
  This method is faster than using a so开发者_StackOverflow中文版rt 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];
    }
  }
}

now I want to have the ability to get the data in randomArray from my other methods. Thanks, Shlomi


You can use property in the class. For example

@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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜