0

I have defined a macro value in Constant.h (#define OK "OK")

And I imported in a First.m file and redefined it (#undef OK, #define OK "Hi")

Then I include Constant.h in Second.m and when I access the "OK" the value is still "OK" not "Hi"

I noticed that the value only changed in First.m.

Just wondering how to change the OK value globally.

Since many .m file are acessing the OK and OK needs to be changed often according to different event

Thanks

Abizern
  • 146,289
  • 39
  • 203
  • 257
pingping
  • 37
  • 1
  • 8

2 Answers2

1
#define OK(str) ISVALIDBOOL(str) ? @"HI" : @"OK"
#define ISVALIDBOOL(str) (str == NO)   // Import in above header 
BOOL str=YES;
NSLog(@"Hi:%@",OK(str));
str=NO;
NSLog(@"Ok:%@",OK(str));

No other way to change the macro at runtime Refer that

Community
  • 1
  • 1
HariKrishnan.P
  • 1,204
  • 13
  • 23
0

You'll need to turn the OK macro from a simple string definition into some conditional statement that tests this special event you talk about. You can only change a macro in the implementation file being compiled; changes are not seen in other compilation units. So the change must be made to the macro in the header file itself.

For example, if the two strings are based on the success of an operation you could do:

#define OK(condition) ((condition) ? @"OK" : @"Failed")

and use it like this:

BOOL success = [self doThing];
NSLog(@"doThing %@", OK(success));

I often define a similar macro to turn BOOLs into NSStrings:

#define STRBOOL(b) ((b) ? @"YES" : @"NO"))
l00phole
  • 622
  • 3
  • 8