Preprocessor macro value to Objective-C string literal

前端 未结 4 658
礼貌的吻别
礼貌的吻别 2021-02-01 04:18

I have a preprocessor macro defined in build settings

FOO=BAR

That value I want to massage into an Objective-C string literal that can be passe

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 04:58

    Here's a modified version of Adam Rosenfield's answer with clearer semantics:

    #define NSStringize_helper(x) #x
    #define NSStringize(x) @NSStringize_helper(x)
    

    I use it to replace code like this:

    case OneEnumValue: name = @"OneEnumValue"; break;
    case AnotherEnumValue: name = @"AnotherEnumValue"; break;
    

    with this:

    #define case_for_type(type) case type: name = NSStringize(type); break
    
    case_for_type(OneEnumValue);
    case_for_type(AnotherEnumValue);
    

提交回复
热议问题