1

In my application I have to redefine a macro. I did like this.

-(void)viewDidLoad{

#undef kMacro
#define kMacro @"New Value"

}

It is working fine within this function. When I put NSLog inside this function, I get "New Value". But however I can't get this new value outside this function or in other classes. (I am getting the Old Value). Is it possible to redefine a macro as global?

Ramaraj T
  • 5,184
  • 4
  • 35
  • 68

1 Answers1

1

When you redefine a macro in a file it is valid only for that file because all files are treated as separate compilation units.

To have it work in other classes you need to put in a header file and #import it in all the files you want to use it.

Better still, don't use macros and use proper C:

const NSString* kMyString = @"New Value";

and then you can access it as a normal external variable in other .m files.

Cthutu
  • 8,713
  • 7
  • 33
  • 49
  • 1
    The declaration should be `NSString * const kMyString`, which is a read-only pointer to `NSString`, rather than a (re-assignable) pointer to a (redundantly) read-only `NSString`. See http://stackoverflow.com/questions/2917941/how-to-declare-nsstring-constants-for-passing-to-nsnotificationcenter – jscs Oct 19 '12 at 18:49