1

i need to define an array as global variable that i can use it all over my code the array must have a data for 10 random number that must be fixed all over the code what i did it the following but with no luck in the .h file after the @interface i put this line

NSMutableArray *globalarray; 

and in the .m file i put this after the @implemention file

- (void)glo { if (!globalarray) { globalarray= [NSMutableArray globalarray]; for (int x = 0; x < 10; x++) { [globalarray addObject:[NSNumber numberWithInt: arc4random()%200]]; }}} 

and on the same .m file i called the array in NSLOG as follows

-(IBAction)click_one:(id)sender{ NSLog(@"%@",globalarray);} -(IBAction)click_two:(id)sender{ NSLog(@"%@",globalarray);} 

the NSLOG returns null in the console any help is highly apprectaied thanks

4
  • 1
    Put [self glo]; to init or viewDidLoad method Commented Aug 25, 2012 at 22:13
  • I might be wrong, but I'm pretty sure you should be declaring the object as extern in the header file and putting the real declaration in a source file. Commented Aug 25, 2012 at 22:27
  • thanks but i recived the below Commented Aug 25, 2012 at 22:27
  • 2012-08-26 01:26:12.891 sort_alg[1192:207] +[NSMutableArray globalarray]: unrecognized selector sent to class 0xe1cc4c 2012-08-26 01:26:12.895 sort_alg[1192:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSMutableArray globalarray]: unrecognized selector sent to class 0xe1cc4c' Commented Aug 25, 2012 at 22:28

3 Answers 3

3

This line:

globalarray= [NSMutableArray globalarray]; 

assumes that there's some method on NSMutableArray called "globalarray". There isn't. You want:

globalarray = [[NSMutableArray alloc] init]; 
Sign up to request clarification or add additional context in comments.

Comments

0
#import "AppDelegate.h" // in viewDidLoad self.del = (AppDelegate*) [[UIApplication sharedApplication] delegate]; // array now available as NSLog(@"%@", del.dataArray); 

Comments

0

Problem is you assigned autorelease allocation.

 GlobalArray=[NSMutableArray array]; is same declarating it as. GlobalArray=[[[NSMutableArray alloc]init]autorelease]; 

// so here reference count increases by 1 when allocating and marking it autorelease makes it reference count to 0. As its reference count becomes 0 it is selected for garbage collection, so you need to retain it, like this

 GlobalArray=[[NSMutableArray array]retain];// when retaining its reference count increases by 1 

Release it when required.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.