Objective-C中的宏调用isEqualToString:产生有关无效令牌的错误 [英] Macro in Objective-C calling isEqualToString: produces error about invalid token

查看:395
本文介绍了Objective-C中的宏调用isEqualToString:产生有关无效令牌的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义这样的宏:

I'm trying to define a macro like this:

#define SOME_DEF [[TTys getString] isEqualToString:ANOTHER_STRING]

然后执行以下操作:

#if SOME_DEF
...
#endif




  • [TTys getString] 返回NSString

  • ANOTHER_STRING 之前定义为 #define ANOTHER_STRINGhello

    • [TTys getString] returns an NSString
    • ANOTHER_STRING is defined earlier as #define ANOTHER_STRING "hello"
    • 我在 #if SOME_DEF 行收到以下错误:


      无效预处理器表达式开始时的标记

      Invalid token at start of a preprocessor expression

      基于此 SO问题这可能是由于在编译时无法解决的问题引起的,但我已经定义了所有内容。我怀疑是
      isEqualToString 方法,但我不知道另一种方法。

      Based on this SO question this might be caused by something that can't be resolved at compile time, but I have everything defined. My suspect is the isEqualToString method, but I don't know of another way to do this.

      推荐答案

      当您编写 #if SOME_DEF 时,预处理器将其解析为:

      When you write #if SOME_DEF the preprocessor resolves it into:

      #if [[TTys getString] isEqualToString:ANOTHER_STRING]
      

      这不是 #if 预处理程序指令的有效条件:

      This is not a valid condition for the #if preprocessor directive:


      '#if'指令允许您测试算术
      表达式的值,而不仅仅是存在一个宏。它的语法是

      The ‘#if’ directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro. Its syntax is

       #if expression
      
       controlled text
      
       #endif /* expression */ 
      

      表达式是整数类型的C表达式,受到严格的限制。它可能包含

      expression is a C expression of integer type, subject to stringent restrictions. It may contain


      • 整数常量。

      • 字符常量,解释为
        将采用普通代码。

      • 用于加法,
        减法,乘法,除法,按位运算,移位,
        比较和逻辑运算(&&和||)的算术运算符。后两者遵守标准C的通常短路规则

      • 宏。在
        表达式值的实际计算开始之前,表达式中的所有宏都扩展了
        中的所有宏。

      • 使用已定义的运算符,让
        检查是否在'#if'中间定义了宏。

      • Integer constants.
      • Character constants, which are interpreted as they would be in normal code.
      • Arithmetic operators for addition, subtraction, multiplication, division, bitwise operations, shifts, comparisons, and logical operations (&& and ||). The latter two obey the usual short-circuiting rules of standard C.
      • Macros. All macros in the expression are expanded before actual computation of the expression's value begins.
      • Uses of the defined operator, which lets you check whether macros are defined in the middle of an ‘#if’.

      不是宏的标识符,它们都被认为是
      数字零。这允许你编写#if MACRO而不是#ifdef
      MACRO,如果你知道定义的MACRO将始终具有
      非零值。没有函数调用
      括号的函数式宏也被视为零。

      Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero.

      来自 GCC文档

      你可以做的是使用运行时评估的常规if语句:

      What you can do instead is using a runtime-evaluated regular if-statement:

      if(SOME_DEF) {
          ...
      }
      

      这篇关于Objective-C中的宏调用isEqualToString:产生有关无效令牌的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆