#define or #if operator error

I read many definitions of the # if and # define operator.
I tried to use the reading method, but only with the error " Invalid token at the beginning of the preprocessor expression " in the line that defines it as a comment below:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#define is_ipad         (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define is_ipad_hd      ( (is_ipad == 1 && [UIScreen mainScreen].bounds.size.width > 1024 ) ? YES : NO)
#define is_iphone_hd    ([UIScreen mainScreen].bounds.size.width > 480 ? YES : NO)
#define device_width    480.0f
#define device_height   320.0f

#if (is_ipad_hd == YES) // Error here
  #define device_width       = 2048.0f
  #define device_height      = 1496.0f
#endif

Why does this work in simple tutorials, and when we try to do something more complex, it happens!

+5
source share
3 answers

These are preprocessor directives, so you do not have access to the [UIScreen mainScreen] methods and to all other objects that are defined during compilation!

+5
source

Macroassessment occurs at compile time.

(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) [UIScreen mainScreen] .

, , . :

BOOL isiPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
BOOL isHD = [[UIScreen mainScreen] scale] == 2.0;

if (isiPad) {
  if (isHD) {
    // retina iPad
  } else {
    // non-retina iPad
  }
} else {
  if (isHD) {
    // retina iPhone/iPod touch
  } else {
    // non-retina iPhone/iPod touch
  }
}
+1

Agree with others here, although I'm not so good at C preprocessor that a quick mail search returned with this:

expression is an integer type expression C with severe restrictions. It may contain .... much better formatting than I can quickly achieve here on the source .

0
source

All Articles