Global variables...

Is this the correct way to declare a global variable in obj-c?

[code]#import <Cocoa/Cocoa.h>

NSMutableArray *coords;

int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}[/code]
I need to access and change this array in several classes and was following the directions here:

http://blog.ablepear.com/2009/12/objective-c-tuesdays-global-variables.html

I was then trying to access the array in this .m file but I keep getting an error message

[code]#import “appController.h”
#import “appImage.h”

@implementation appController

-(void)awakeFromNib {
//NSImage *imageFromBundle = [NSImage imageNamed:@“fatelvis.png”];
//[imageView setImage: imageFromBundle];

//declare the array -->THIS IS THROWING AN ERROR
coords = [NSMutableArray new];

//make new image
int i;

for(i=0; i<2; i++) {
	
	//see if we've made an elvis yet
	itemcount =  [coords count]; 
	
	if (itemcount == 0) {
		//add object to array
		NSLog(@"first loop");
		[coords	addObject:  [NSNumber numberWithInt:0]];
		[coords	addObject:  [NSNumber numberWithInt:0]];
	} else { 
		NSLog(@"second loop");
		//get coords
		NSString *xpos = [coords objectAtIndex:0];
		NSString *ypos = [coords objectAtIndex:1];
		NSLog(@"The coordinates of the object are X: %@ Y: %@", xpos, ypos);
		
	}
	
	appImage *x = [appImage new];
	[x setImage: @"test1" getImage: @"fatelvis.png"];
}

}

@end

error coords undeclared (first use in this function)

Any help would be appreciated.[/code]

I figured it out. I think.

Declare your globals in the main.m file like such:

[code]#import <Cocoa/Cocoa.h>

//globals
NSMutableArray *coords;

int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}[/code]
then in any .m file you’d like to use the var you need to declare them but with the extern prefix, which let’s the compiler know that these vars are external to this file and have already been declared, your not duplicating stuff and to accept the values (if any). At least that’s how I interpreted it and it worked for me. People with more experience feel free to correct any mistakes I’m making here or point out an easier way…

//globals
extern NSMutableArray *coords;

Hi,

global (object) variables in an object-oriented language are the worst case, because it defeats the philosophy of encapsulated data.

In most cases instance or class variables with respective accessor methods or passing variables while initializing an instance are preferable.

In your case you could probably use an instance variable, which must be declared and initialized properly.
Other classes can set and get the variable by accessor methods or directly, if the variable is defined as a property. Or you could pass the variable thru an init method of a custom class.

What kind of memory management do you use?

The normal way to create an instance of a class is to use its [[class alloc] init] method with a balancing release method in the dealloc method.
Using init methods like new or copy also gives ownership to the calling class therefore this class is responsible to release the memory after finishing with it.