Objective C - How to use extern variables?

Up vote 2 down vote favorite share g+ share fb share tw.

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; objective-c global-variables extern link|improve this question edited Apr 16 '11 at 16:38John Topley33.1k1569119 asked Dec 5 '10 at 18:08aryaxt3,42631467 93% accept rate.

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

1 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 5 '10 at 18:54.

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.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions