What does "macro" mean in Objective-C? What does "macro" mean in Objective-C? objective-c objective-c

What does "macro" mean in Objective-C?


Yes, Larme is right. Macros can be used in many languages, it's not a specialty of objective-c language.

Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro. It doesn’t do anything more clever than that.

Almost literal code substitution. e.g.-

Suppose you want a method to return the maximum of two numbers. You write a macro to do this simple task:

#define MAX(x, y) x > y ? x : y

Simple, right? You then use the macro in your code like this:

int a = 1, b = 2;int result = 3 + MAX(a, b);

EDIT:

The problem is that the preprocessor substitutes the macro definition into the code before compilation, so this is the code the compiler sees:

int a = 1, b = 2;int result = 3 + a > b ? a : b;

C order of operations requires the sum 3 + a be calculated before the ternary operator is applied. You intended to save the value of 3 + 2 in result, but instead you add 3 + 1 first, and test if the sum is greater than 2, which it is. Thus result equals 2, rather than the 5 you expected.

So you fix the problem by adding some parentheses and try again:

#define MAX(x, y) ((x) > (y) ? (x) : (y))


A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants.

You create macros with the ‘#define’ directive. ‘#define’ is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro's body, expansion or replacement list. For example,

 #define BUFFER_SIZE 1024

defines a macro named BUFFER_SIZE as an abbreviation for the token 1024. If somewhere after this ‘#define’ directive there comes a Objective C statement of the form

 foo = (char *) malloc (BUFFER_SIZE);

The Objective C compiler will see the same tokens as it would if you had written

 foo = (char *) malloc (1024);

You can also define macros whose use looks like a function call. These are called function-like macros. To define a function-like macro, you use the same ‘#define’ directive, but you put a pair of parentheses immediately after the macro name. Like:

#define isIphone([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)#define GetImage(imageName) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]     pathForResource:imageName ofType:@"png"]]


Macros are compile time constants. That means they will replaced with actual values in the compile time.

#define MIN_VALUE 3 // Definitionif(x > MIN_VALUE) // Usage{}

While compiling it actually looks like

 if(x  > 3) // During compilation  {  }