How to use iOS definition macros

I need to make a determination, but if I use an iPad or iPhone, in this case I need different values.

So, I think it should look like this:

#ifdef UI_USER_INTERFACE_IDIOM == iPAD #define ROWCOUNT 12 #else #define ROWCOUNT 5 #endif 

Is there any solution to get it?

+4
source share
3 answers

#ifdef really does not do what you want here.

A good solution would be:

 #define ROWCOUNT ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 5 : 12) 
+7
source

UI_USER_INTERFACE_IDIOM is a macro that expands to some expression that checks the type of equipment at runtime, and not at compile time. So you want to change your definition of ROWCOUNT as a variable, not a constant or a macro.

 NSUInteger rowCount; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) rowCount = 12; else rowCount = 5; 

or more briefly:

 NSUInteger rowCount = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 12 : 5; 
+7
source

This approach correctly determines the availability of UI_USER_INTERFACE_IDIOM:

 #ifdef UI_USER_INTERFACE_IDIOM #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #else #define IS_IPAD false #endif #define ROWCOUNT (IS_IPAD ? 12: 5) 

You can also use the IS_IPAD macro, as shown below:

 NSUInteger rowCount; if (IS_IPAD){ rowCount = 12; }else{ rowCount = 5; } 
+5
source

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


All Articles