0

I would like to define and set a variable to the value x in all my classes, excepted in a given class.

Thus, I have added to the file MyApp-Prefix.pch :

int ddLogLevel = LOG_LEVEL_VERBOSE;

Now, in a given class, I want to change this variable. What should I do ? Where should I put the new commands ? In the .m ? Before the @implementation ? With/without const and/or static and/or the type ??


PS : As you saw it, I don't understand anything to the extern/const thing. If anyone knows a good and clear reference, for a newbie who doesn't know C, I'll take it !

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Colas
  • 3,473
  • 4
  • 29
  • 68
  • 1
    what you want is a global variable right? take a look at [this answer](http://stackoverflow.com/questions/8808159/objective-c-global-variables) – Fonix Apr 16 '13 at 15:18
  • Yes it is global variable, set to a value in Prefix.pch ans which I can override in a given class. – Colas Apr 16 '13 at 15:32

1 Answers1

2

I recognize ddLogLevel as the log level for the log system cocoalumberjack. Instead “overriding” for your class (no such thing since it is a global variable), you should do what they call Fine Grained Logging. That is, define a log with using the next bit in the bitmask, then set the global constant to a bitwise combination of masks that includes or excludes your flag. It's further explained on the linked page. Example:

Add these defines

#define LOG_FLAG_FOOD_TIMER    (1 << 4)  // 0...0010000
#define LOG_FOOD_TIMER  (ddLogLevel & LOG_FLAG_FOOD_TIMER)
#define DDLogFoodTimer(frmt, ...)   ASYNC_LOG_OBJC_MAYBE(ddLogLevel, LOG_FLAG_FOOD_TIMER,  0, frmt, ##__VA_ARGS__)

then in your class use this macro as the log statement:

DDLogFoodTimer(@"blah");

and set the global as:

static const int ddLogLevel = LOG_LEVEL_WARN | LOG_FLAG_FOOD_TIMER;

In C and Objective-C the const keyword creates a read only variable. The keyword const applies applies to whatever is immediately to its left. If there is nothing to its left, it applies to whatever is immediately to its right. Example:

// a modifiable pointer to a constant integer (its value can't be modified). 
const int * i;

// Constant pointer to constant integer. 
const int const * i;

extern is an indication that a variable is defined elsewhere. It's common style to define global variables like this:

// header file (outside the class definition)
extern NSString* PREFS_MY_CONSTANT;

// implementation file (outside the class definition)
NSString* PREFS_MY_CONSTANT = @"prefs_my_constant";
Jano
  • 62,815
  • 21
  • 164
  • 192
  • thanks ! First, did you mean "declared elsewhere" instead of "defined elsewhere" ? – Colas Apr 17 '13 at 07:40
  • Second : I already saw this solution but it is not what I am looking for, since I dont want to use a new name for my logger. My idea about that would be to redefine DDLogVerbose() to nothing is my class, with the help of #define. Would you have a hint for that ? Thanks ! – Colas Apr 17 '13 at 07:43
  • 1
    I meant is declared where the `extern` is, but defined (set) elsewhere. You can't have a `#define` per class because their effects are global. You would have to pass `__PRETTY_FUNCTION__` (which translates to something like `-[SomeClass someMethod]`) to the logger, and then discard the statement based in the class name. – Jano Apr 17 '13 at 08:26
  • About `extern`, do you confirm that it can't be defined in two different places ? – Colas Apr 17 '13 at 08:51