0

我正在制作一个 iPhone 应用程序,我想做宽屏检测,所以我做了一堆#define's,我想用它做一个 if 语句。

// Device and Widescreen Detection
#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

// iPhone
#define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] )  || ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone Simulator" ] ) )
#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN )

// iPod Touch
#define IS_IPOD   ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPod touch" ] )
#define IS_IPOD_5 ( IS_IPOD && IS_WIDESCREEN )

// iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

我想做一个 if 语句说

if (IS_IPHONE_5) {
...
} else {
...
}

问题是 if 语句返回一个错误,XCode 一直告诉我这样做

if (IS_IPHONE_5 {
...
} else {
...
}

或这个

if IS_IPHONE_5 {
...
} else {
...
}

否则会出错。写这个的正确方法是什么?

4

1 回答 1

14

您在IS_IPHONE宏定义中缺少括号。你有:

( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] )  || ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone Simulator" ] ) )

但你应该有:

( ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] )  || ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone Simulator" ] ) )

要查找此类错误,您可以轻松使用菜单Product -> Generate Output -> Preprocessed File中的命令。这样,您的预处理器宏将被扩展,您将能够看到最终代码,因此错误在哪里。

在此处输入图像描述

于 2013-07-31T02:56:23.523 回答