Ramaraj T
Ramaraj T

Reputation: 5230

Redefining Macros

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?

Upvotes: 1

Views: 156

Answers (1)

Cthutu
Cthutu

Reputation: 8907

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.

Upvotes: 1

Related Questions