2 loveiflower loveiflower 于 2013.07.31 15:25 提问

if语句中放一个#define

开发一个iPhone应用,需要做宽屏检测,用到许多#define,想要放到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语句会返回错误:

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

或者:

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

为什么会报错?应该怎么正确实现?

2个回答

karas117
karas117   2013.08.02 11:44

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

最后的地方多了个括号啊!!!!!!!!

u011418969
u011418969   2013.08.07 10:41

**可以考虑用 条件编译 加 宏编译,
#if IS_IPHONE_5
. . .
#elif 条件
. . .
#else
. . .
#endif

Csdn user default icon
上传中...
上传图片
插入图片