Objective C - How to use extern variables?

aryaxt picture aryaxt · Dec 5, 2010 · Viewed 19k times · Source

I am trying to use extern variables.

It complains that because of using numberWithInt I am not passing a contants as the value of my variable

So I removed the const and it's complaining that an extern variable must be a constant, so what is the solutions here?

I DO NOT WANT TO USE INT

.h
extern NSNumber const *MoveID;

.m
NSNumber const *MoveID = [NSNumber numberWithInt:1];

Answer

beefon picture beefon · Dec 5, 2010

You can try to do the following:

.h

extern NSNumber *MoveID;

.m

NSNumber *MoveID;
@implementation MYGreatClass
+ (void) initialize {
    static bool done = FALSE;
    if(!done){ // This method will be called again if you subclass the class and don't define a initialize method for the subclass
        MoveID = [[NSNumber numberWithInt:1] retain];
        done = TRUE;
    }
}