【问题标题】:Make a macro: how to embed string correctly制作宏:如何正确嵌入字符串
【发布时间】:2016-04-05 07:08:53
【问题描述】:
#define SDNSPredicate(key,value) \
    [NSPredicate predicateWithFormat:@"#key == %@",value];

当我使用SDNSPredicate(@"hCName",@"ccc") 时,我期望hCName == "ccc" 但它变成key == "ccc"

如何做到正确?

【问题讨论】:

    标签: objective-c macros


    【解决方案1】:

    如何做到正确?

    使用函数。 Macros are evil.

    static inline NSPredicate *SDNSPredicate(NSString *key, NSString *value) {
        return [NSPredicate predicateWithFormat:@"%@ == %@", key, value];
    }
    

    【讨论】:

      【解决方案2】:

      解决方案:

      #define SDNSPredicate(key,value) \
      [NSPredicate predicateWithFormat:@"%K == %@",key,value];
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 2017-02-18
      • 2019-12-13
      相关资源
      最近更新 更多