What does “macro” mean in Objective-C?

前端 未结 4 1489
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 17:35

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

I have found that \"macro\" is used with #define but still do not get its meanin

相关标签:
4条回答
  • 2021-02-02 17:48

    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))
    
    0 讨论(0)
  • 2021-02-02 17:55

    Wikipedia has the answer, under Macro.

    Definition:

    The term originated with macro-assemblers, where the idea is to make available to the programmer a sequence of computing instructions as a single program statement, making the programming task less tedious and less error-prone.

    Usage:

    Keyboard and mouse macros that are created using an application's built-in macro features are sometimes called application macros. They are created by carrying out the sequence once and letting the application record the actions. An underlying macro programming language, most commonly a Scripting language, with direct access to the features of the application may also exist.

    0 讨论(0)
  • 2021-02-02 18:00

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

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

    While compiling it actually looks like

     if(x  > 3) // During compilation
      {
    
      }
    
    0 讨论(0)
  • 2021-02-02 18:05

    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"]]
    
    0 讨论(0)
提交回复
热议问题