Using #define in an if statement

Is it possible to use #define in an if statement? The following code works, but I get a macro redefinition warning.

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { #define TableViewHeight 916 #define DisplayHeight 1024 #define DisplayWidth 768 } else { #define TableViewHeight 374 #define DisplayHeight 480 #define DisplayWidth 320 } 

I tried this too, but this did not work:

 #ifdef UIUserInterfaceIdiomPad #define TableViewHeight 916 #define DisplayHeight 1024 #define DisplayWidth 768 #else #define TableViewHeight 374 #define DisplayHeight 480 #define DisplayWidth 320 #endif 

Any ideas?

+4
source share
6 answers

Yes, it is possible, but it probably does not do what you think. Preprocessor directives are interpreted before the results of the preprocessing stage are compiled.

This means that all preprocessor directives are interpreted by overriding some macros before the rest of the code is compiled, which will look something like this.

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { } else { } 

In other words, after preprocessing, you just have the if and else tags.

If you want to change the value of something based on a condition at runtime, then something must be a genuine object, not just a preprocessor macro. For instance.

 extern int TableViewHeight; // defined somewhere else extern int DisplayHeight; // defined somewhere else extern int DisplayWidth; // defined somewhere else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { TableViewHeight = 916; DisplayHeight = 1024; DisplayWidth = 768; } else { TableViewHeight = 374; DisplayHeight = 480; DisplayWidth = 320; } 
+7
source

#define (and #ifdef , etc.) are processed by the preprocessor. This runs all the way to the compiler; they are really two completely separate processes. Therefore, it does not interact with constructions like if .

The second code example may work if UIUserInterfaceIdiomPad is a macro (i.e. it was #defined ).

+1
source

I think you answered your question :) To remove warnings, why not declare them as variables instead of macros?

I would do this:

 CGFloat tableHeight = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 916.0f : 374.0f; 
+1
source

#defines are replaced by the preprocessor before starting the compiler. This means that you cannot set their value at runtime. The way I can do what you are going for is something like this:

In your .h file:

 static CGFloat TableViewHeight; static CGFloat DisplayHeight; static CGFloat DisplayWidth; 

in the .m file (class implementation):

 + (void)initialize { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { TableViewHeight = 916.0; DisplayHeight = 1024.0; DisplayWidth = 768.0; } else { TableViewHeight = 374.0; DisplayHeight = 480.0; DisplayWidth = 320.0; } } 

Alternatively, you can do something like this:

 #define TableViewHeight ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 916.0 : 374.0) #define DisplayHeight ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 1024.0 : 480.0) #define DisplayWidth ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 768.0 : 320.0) 

Finally, for DisplayHeight and DisplayWidth, you really don't need to do anything and it is probably best to use CGRectGetHeight([[UIScreen mainScreen] bounds]) and CGRectGetWidth([[UIScreen mainScreen] bounds]) .

+1
source

Use #if instead of the usual if .

However , what would be a preprocessor, which would mean that your application would just compile only for the iPhone or iPad (if it even works).

Bottom line: no, you cannot use macros. They are checked at compilation, and not at application startup time.

0
source

No Unfortunately. #define used by the preprocessor to replace characters before compiling the code, and if is part of the actual code that needs to be compiled. In the first case, the preprocessor knows nothing about what will be the result of your if statement.

The second case does not make sense, because it doesn’t matter to you, the UIUserInterfaceIdiomPad is determined, you care if this has the value UI_USER_INTERFACE_IDIOM() .

0
source

Source: https://habr.com/ru/post/1388382/


All Articles