问题宏(#define)"显示数字常量之前的预期标识符"错误,在iPad中

Har*_*sad 7 iphone macros ipad

我正在开发一个应用程序,我需要定义几个将在多个类中使用的常量.我已经在一个.h文件中定义了所有常量(比如说"constants.h")并在myAppName_Prefix.pch文件中导入了该文件位于项目的"其他来源"文件夹中.使用这些常量的类正在编译时没有任何错误,但其他类,我声明了一些UISwipeGestureRecognizers,将错误称为" 数字常量之前的预期标识符 "这是代码片段来自其中一个显示错误的类:

if (gesture.direction==UISwipeGestureRecognizerDirectionLeft)
Run Code Online (Sandbox Code Playgroud)

我将我的常量定义为:

#define heading 1
#define direction 2
#define statement 3
#define refLink 4
#define correctResponse 5
#define incorrect1Response 6
Run Code Online (Sandbox Code Playgroud)

如果我在每个班级中单独定义它们,那么一切都工作正常.任何人都可以建议我如何解决这个问题.

Mat*_*uch 5

预处理代码后

if (gesture.direction==UISwipeGestureRecognizerDirectionLeft)
Run Code Online (Sandbox Code Playgroud)

看起来像这样

if (gesture. 2==UISwipeGestureRecognizerDirectionLeft) 
Run Code Online (Sandbox Code Playgroud)

这显然不是有效代码。

解决方案是在#defines 前面放置一个唯一的命名空间字符串。

#define hariDirection 2
Run Code Online (Sandbox Code Playgroud)

或者

#define kDirection 2
Run Code Online (Sandbox Code Playgroud)

或者恕我直言最好的解决方案:不要使用 #define

typedef enum {
    heading = 1,
    direction,
    statement,
    refLink,
    correctResponse,
    incorrect1Response,
} MyDirection;
Run Code Online (Sandbox Code Playgroud)

这将做同样的事情,但它不会与其他方法和变量名称发生冲突。