user3046185 user3046185 - 4 months ago 44
Objective-C Question

iOS preprocessing conditional with defined macros causes "token is not valid"

I have a class with dimensions written in consts for all UI elements used in my app. Suddenly I noticed that things looks different on 6 and 4 iphone, so I need to adjust numbers slightly. But when i want to use conditionals to detect device and assign correct values to constants, it gives me "token is not a valid binary operator in a preprocessor subexpression".
Code below:

#import <UIKit/UIKit.h>
#import "UIStyles.h"

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

@implementation UIStyles

#if (IS_IPHONE_4_OR_LESS)
const int BOTTOM_BTN_HEIGHT = 40;
const int PRICE_AND_LOCATION_BAR_HEIGHT = 40;
#else
const int BOTTOM_BTN_HEIGHT = 50;
const int PRICE_AND_LOCATION_BAR_HEIGHT = 50;
#endif

@end

Answer

If you want to use #define preprocessor result with #if preprocessor you should try like this

#if defined(IS_IPHONE_6)
const int BOTTOM_BTN_HEIGHT = 40;
const int PRICE_AND_LOCATION_BAR_HEIGHT = 40;
#else
const int BOTTOM_BTN_HEIGHT = 50;
const int PRICE_AND_LOCATION_BAR_HEIGHT = 50;
#endif

Out side you can try like this:

 NSLog(@"Botton Height : %d",IS_IPHONE_6);
Comments