0

I have two simple constants:

  1. NSString and
  2. unichar,

defined as follows:

static NSString * const string = @"\u2022";
static unichar const character = 0x2022;

I'd want to have the number 0x2022 defined in one place.

I tried a lot of combinations (# and ##, also CFSTR macro) with no success.

Can it be done?

Rudolf Adamkovič
  • 30,008
  • 11
  • 100
  • 116

1 Answers1

1

(Using ideas from How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?):

#define STRINGIFY(_x_) #_x_

#define UNICHAR_FROM_HEXCODE1(_c_) 0x ## _c_
#define UNICHAR_FROM_HEXCODE(_c_) UNICHAR_FROM_HEXCODE1(_c_)

#define NSSTRING_FROM_HEXCODE1(_c_) @"" STRINGIFY(\u ## _c_)
#define NSSTRING_FROM_HEXCODE(_c_) NSSTRING_FROM_HEXCODE1(_c_)

#define MYCHARCODE 2022
static unichar const character = UNICHAR_FROM_HEXCODE(MYCHARCODE);
static NSString * const string = NSSTRING_FROM_HEXCODE(MYCHARCODE);

Preprocessed output:

static unichar const character = 0x2022;
static NSString * const string = @"" "\u2022";

(Note that @"" "\u2022" is equivalent to @"\u2022".)

Community
  • 1
  • 1
Martin R
  • 510,973
  • 84
  • 1,183
  • 1,314