11

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];
John Topley
  • 113,588
  • 46
  • 195
  • 237
aryaxt
  • 76,198
  • 92
  • 293
  • 442

4 Answers4

15

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;
    }
}
beefon
  • 3,194
  • 1
  • 21
  • 25
  • 7
    Note that the value of `MoveID` isn't gonna be set until something somewhere touches the `MYGreatClass` class. You *could* use a `+load` method if that is a problem. – bbum Dec 05 '10 at 18:54
3

As @BoltClock said, you cannot set a non-constant value to be of const type.

What you could do is this:

extern NSNumber *MoveID;

And...

NSNumber *MoveID;
@implementation SomeClass 
static BOOL loaded = NO;
+ (void) initialize {
   if(!loaded) {
      MoveID = [[NSNumber alloc] initWithInt:1];
      loaded = YES;
   }
}
//blah blah blah

@end
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
2

EDIT: I just realized that I totally missed the question and was going on about why the error was occurring, oops. I'll leave the first part of my answer here though because Jacob Relkin quotes it in his answer.


Because [NSNumber numberWithInt:1] is not a compile-time constant value, you cannot set an NSNumber created with it to a const variable.

There appears to be a radar about extern NSNumber consts, which seem to be unsupported in Objective-C. I guess you can use a preprocessor macro to create NSNumbers from constant ints or floats as described in this article. It's not nearly the same as what you intend but it seems to be pretty close.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

Just for completeness, the modern method is do do it as:

in .h

extern NSNumber *MoveID;

in .m

NSNumber *MoveID;

...

- (void)viewDidLoad {
    [super viewDidLoad];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        MoveID = @1;
    });

    ...
}

dispatch_once() will only ever run once so the initialiser is not duplicated, and it is thread safe. Also, pushing down initialisation code lower down in the view lifecycle.

Abizern
  • 146,289
  • 39
  • 203
  • 257