Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:34:27+00:00 2026-05-25T15:34:27+00:00

I have created a mutable array called numbers and he holds up 20 diffrent

  • 0

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:34:27+00:00Added an answer on May 25, 2026 at 3:34 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that was created from data coming from a mutable array.
I have created a custom button called ModuleUIButton which inherits from UIButton . This
I have created a few small flash widgets that stream .mp3 audio from an
I have a mutable array of custom objects. I want to filter that array
I am trying a simple application where I have a mutable array of mutable
I have created a button action method. Initialized an NSDictionary with values from an
I need to create a mutable two-dimensional array in Objective-C. For example I have:
I have a mutable array which stores sprites but the problem is that the
I'm trying to remove some objects from a ListBox that I have created, and
So here's how I do the search: I have an array of Data and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.