What does "macro" mean in Objective-C?

Am I new to iOS development and I just want to know the meaning of the macro in Objective-C?

I found that "macro" is used by C # define, but still does not make sense.

http://www.saturngod.net/ios-macro-define-value-with-condition

+7
macros objective-c
source share
4 answers

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

Macros are preprocessor definitions. This means that before compiling the code, the preprocessor scans your code and, among other things, replaces the definition of your macro wherever it sees the name of your macro. He does nothing smarter than that.

Almost literal code change. eg.-

Suppose you want a method to return a maximum of two numbers. You are writing a macro to accomplish this simple task:

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

Simple, right? Then you use the macro in your code as follows:

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

EDIT:

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

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

In the order of operations, it is required that the sum 3 + a be calculated before applying the triple operator. You intended to keep the 3 + 2 value as a result, but instead, first add 3 + 1 and check if the amount exceeds the amount of 2, which is. So the result is 2, not 5, which you expected.

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

 #define MAX(x, y) ((x) > (y) ? (x) : (y)) 
+17
source share

A macro is a piece of code that is given a name. Whenever a name is used, it is replaced by the contents of the macro. There are two types of macros. They differ mainly in what they look like when they are used. Object-like macros resemble data objects when used, macros like functions resemble function calls.

An object-like macro is a simple identifier that will be replaced by a piece of code. It is called object-like because it looks like a data object in the code that uses it. They are most often used to denote symbolic names in numeric constants.

You create macros with the #define directive. '#define is followed by the name of the macro, and then the sequence of tokens is an abbreviation that is referred to differently as a list of macros, extension or replacement. For example,

  #define BUFFER_SIZE 1024 

defines a macro called BUFFER_SIZE as an abbreviation for the token 1024. If, somewhere after this #define directive, an Objective-C instruction of the form appears

  foo = (char *) malloc (BUFFER_SIZE); 

The Objective-C compiler will see the same tokens as if you wrote

  foo = (char *) malloc (1024); 

You can also define macros whose use looks like a function call. They are called functional macros. To define a functionally similar macro, you use the same #define directive, but immediately after the macro name you put a pair of parentheses. For example:

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

Wikipedia has the answer in the Macro section.

Definition:

The term begins with macroassemblers, where the idea is to provide the programmer with a sequence of computational instructions in the form of a single program operator, which makes the programming task less tedious and less error prone.

Application:

Keyboard and mouse macros created using the built-in application macros are sometimes called application macros. They are created by executing the sequence once and allow the application to record actions. There may also be a basic macro programming language, most often a scripting language, with direct access to application functions.

0
source share

Macros are compile-time constants. This means that they will be replaced with actual values ​​at compile time.

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

When compiled, it looks like

  if(x > 3) // During compilation { } 
0
source share

All Articles