Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use define macros iOS

Tags:

ios

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

So, as I think it should looks like this:

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

Is there some solution to get it?

like image 315
Matrosov Oleksandr Avatar asked Aug 06 '13 20:08

Matrosov Oleksandr


People also ask

How do you write #define in Swift?

Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let FADE_ANIMATION_DURATION = 0.35 .

What is macros in iOS?

What Are Macros? In the context of iOS, a macro is a phrase or text string that can be inserted by the system keyboard automatically when triggered by a user pressing a short, specific sequence of keys.

How do I declare macros?

A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. Here, when we use c in our program, it is replaced with 299792458 .


1 Answers

UI_USER_INTERFACE_IDIOM is a macro that expands to some expression that checks the type of hardware at runtime, not at compile time. Thus you would want to change your definition of ROWCOUNT to be a variable rather than a const or macro.

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

or more concisely:

NSUInteger rowCount = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 12 : 5;
like image 74
Mike Mertsock Avatar answered Oct 03 '22 22:10

Mike Mertsock