13

I know how to do it for a float:

#define kMyConstant 1.0f

but how could I do that fora BOOL value?

3 Answers3

45

#define kMyConstantBOOL YES

Or

static BOOL MyConstantBool = YES;

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 2
    I like the latter as you get some type checking and can view it and use it as a symbol in the debugger. – nall Nov 02 '09 at 18:34
  • 2
    did you mean "const" rather than "static"? static BOOL is a variable. – progrmr Nov 05 '09 at 01:26
  • @progrmr would static const be appropriate? Correct me if I'm wrong, but my understanding is static means there's only one instance of the variable, and const means it can't change. So static const will make sure it's always the same value and doesn't take up extra memory with new instances? – Jake T. Nov 15 '16 at 16:17
  • `static` specifies the scope and lifetime of the variable ([more details](http://stackoverflow.com/a/1795448/1693173)), whereas `const` specifies that it can't be changed. `BOOL const MyConstantBool = YES;` would be preferred. – progrmr Nov 16 '16 at 00:16
8

Here's an example:

#define kMyConstant      YES
#define kMyOtherConstant NO
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
7

I would do it differently:

enum { MyConstantBool = YES };

It's a constant, it doesn't take up storage space.

progrmr
  • 75,956
  • 16
  • 112
  • 147