重复数据删除NSString& unichar常数

时间:2013-08-19 17:56:55

标签: c++ objective-c c unicode c-preprocessor

我有两个简单的常量:

  1. NSString
  2. unichar
  3. 定义如下:

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

    我想在一个地方定义数字 0x2022

    我尝试了很多组合(###,还有CFSTR宏)但没有成功。

    可以吗?

1 个答案:

答案 0 :(得分:1)

(使用来自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);

预处理输出:

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

(请注意,@"" "\u2022"相当于@"\u2022"。)