Macro and const

The introduction of the macro

Macros, a term for batch processing, simply replace text according to defined rules

The replacement process is during compilation of the program, so heavy use of macros can cause compilation to take too long

security

Macros do not perform security checks during replacement, so be aware of “edge effects”

For example

#define N 1+2 NSInteger a = N/2; // Expected result a = 1.5Copy the code

The reason is that a = N/2 = 1+2/2 = 2 during the substitution, so we recommend using parentheses when defining macros to represent them as a whole

The classification of the macro

It can be divided into object macros and function macros, as well as macros with and without parameters

  • The object of macro

    #define AGE 20
    Copy the code
  • The function of macro

    #define MAX(X,Y) ((X)>(Y)? (X):Y))Copy the code

    Notice that this is just notice the edge effect, so we put parentheses

The use of the macro

  1. Character is changed

    If you want to use strings directly you can add a “#”

    #define STR(x) #x NSLog(@"str:%s",STR(aaa)); // Output: STR :aaaCopy the code

    Use “##” to connect

    #define function(name) +(instanceType)function##name function(getCopy the code
    #define PARSER(N) printf("token" #N " = %d\n", token##N) int token64 = 64; int token32 = 32; PARSER(64); // Print token64 PARSER(32); Token64 = 64 //token32 = 32Copy the code
  2. Use ARC and MRC

    #if __has_feature(objc_arc)
    // ARC
    #else
    // MRC
    #endif
    Copy the code
  3. Macros with arguments are called not only to expand the macro, but also to replace the parameter with an argument

    General form #define Macro name (parameter list) String

    The common one is NSLog. In project development, we will use NSLog many times to print parameters and view results, but we do not want to have these NSLog functions when publishing, but it is not realistic to delete a large project one by one, so we can use a custom NSLog

    #ifdef DEBUG #define LRLog(...) NSLog (@ "% s \ n line % d % @ \ n \ n", __func__, __LINE__, [nsstrings stringWithFormat: __VA_ARGS__]) # else # define LRLog (...). LRLog(@"111"); // Print the result main, line 25, 111Copy the code

    ##__VA_ARGS__ : Pass at least one argument


Introduce const

Const is a read-only keyword. Once used to determine the content or pointer, it cannot change the content or pointer that has been determined

Const usage

Basic data types

// const a = 10; const int b = 20;Copy the code

Const and pointer types

The limit varies depending on the position of const

For example

When in the foreground, indicates that the pointer is not allowed to change, the value can change

const NSString *namePoint1 = @"test";
Copy the code

In the middle, the pointer is not allowed to be modified, the value can be changed

NSString const *namePoint2 = @"test";
Copy the code

At the end, the delegate value cannot be changed, and the pointer can be changed (most often)

NSString * const namePoint3 = @"test";
Copy the code

experience

The most recent content to the right of const cannot be changed

The easiest way to do this is to remove type viewing

const *namePoint1 = @"test"; const *namePoint2 = @"test"; * const namePoint3 = @"test"; // Const namePoint3 is immutable, valueCopy the code

Const can save space and avoid unnecessary memory allocation. Compilers usually don’t allocate storage for ordinary const constants. Instead, they are stored in a symbol table. This makes them compile-time constants and makes them efficient without storing or reading memory


conclusion

The advantages of the macro

  • Improve the readability of the program, but also easy to modify, users only need to define in one place, use more than one place, modify also only need to modify one place
  • Improve the running efficiency of the program: using the macro definition with parameters can not only complete the function of function call, but also avoid the operation of function out and on the stack, reduce the system overhead and improve the running efficiency. If a function is frequently used in the project, you can consider the macro definition

The disadvantage of the macro

  • Too many macros can lead to relatively long compilation times
  • No security checks

The difference between macros and const

#define and const constants can be used to modify constants

  • The compiler handles it differently

    • Macros are deployed during the preprocessing phase
    • Const is used at compile run time
  • The type and security check are different

    • Macros simply expand, as many times as they are used, without allocating memory
    • Const is allocated in memory (even on the stack), is global, has only one copy of memory, and checks for errors
  • Can you define code

    • Macros define code
    • Const cannot define code
  • Compile time

    • Too many macros can lead to relatively long compilation times

    • Const is compiled only once, shortening compilation time

In development, it is common to define a constant string as const and code as macro


Refer to the blog

www.jianshu.com/p/fb1cdeb2e…

Juejin. Cn/post / 684490…

www.jianshu.com/p/9d31c849d…

www.jianshu.com/p/213b3b96c…