user3046185
user3046185

Reputation: 139

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

Upvotes: 1

Views: 926

Answers (2)

Vishnuvardhan
Vishnuvardhan

Reputation: 5107

Try the below way...

#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);

Upvotes: 2

Eugeny Sychev
Eugeny Sychev

Reputation: 1

I suppose that you shoud create a function for separate this devices. Seems that your compiller couldn't call [[UIScreen mainScreen] bounds].size.width on preprocessing). This is not calling for replace as I think...

Upvotes: 0

Related Questions