How to use const (constant), define (macro) correctly

foreword

In development, we may often use macro definitions, or use const to modify some data types. Often developers do not know how to use them correctly, resulting in indiscriminate use of macro definitions and const modifiers in projects. This article mainly introduces how to correctly use constanddefine(宏定义)

When we want to define some data that is shared globally, such as notification name, animation duration, etc., we can use , 常量, 变量:

  • Macro:

        // 注意后面不需要带符号
        #define ScottDidLoginSuccess @"登陆成功"
  • variable:

        // 注意后面一定要带符号
        NSString *scottDidLoginSuccess = @"登陆成功";
  • constant:

        // 四种写法
        static const NSString *scottDidLoginSuccess = @"登陆成功";
        const NSString *scottDidLoginSuccess = @"登陆成功";
        NSString const *scottDidLoginSuccess = @"登陆成功";
        NSString *const scottDidLoginSuccess = @"登陆成功";

So the question is, how do we choose?
Let me first explain what I understand between them:

  • Macro: Just perform text replacement in the preprocessing stage, without type, without any type checking, the compiler can optimize the same string and save only one copy to the data segment. Even strings with the same suffix can be optimized, you can use GCC to compile the test, Hello worldwith worldtwo strings, and only store the first one. When fetching, you only need to give the front and middle addresses. If it is an integer type or a floating point type, there will be multiple copies, but these numbers are written in the instructions, which only account for code fragments. Moreover, a large number of macros will cause the binary file to become larger.
  • Variable: share a piece of memory space. Even if it is used in N places in the project, it will not allocate N pieces of memory space. It can be modified and type checked at the compilation stage.
  • Constant: Share a piece of memory space. Even if it is used in N places in the project, it will not allocate N pieces of memory space. It can be modified according to constthe modified location settings, and type checking is done at the compilation stage.

Constant distinction

  • Global constants: no matter what folder you define, it can be accessed externally

        const NSString *scottDidLoginSuccess = @"登陆成功";
  • Local constants: After being modified with static, they cannot be accessed by the outside world

        static const NSString *scottDidLoginSuccess = @"登陆成功";

const modification position is different, what does it mean

    // 1.
    const NSString *scottDidLoginSuccess = @"登陆成功";
    // 2.
    NSString const *scottDidLoginSuccess = @"登陆成功";
    // 3.
    NSString * const scottDidLoginSuccess = @"登陆成功";

At first glance, WTF, aren't they all the same, in fact, they are not the same, let's explain what each represents

  1. *scottDidLoginSuccesscannot be modified, scottDidLoginSuccesscan be modified
  2. *scottDidLoginSuccessCan not be modified, scottDidLoginSuccesscan be modified, which is the same as the first case
  3. scottDidLoginSuccessCan not be modified, *scottDidLoginSuccesscan be modified.

Conclusion:const The total on the right cannot be modified

So we generally define a constant and don't want to be modified, it should be defined like this:
NSString *const scottDidLoginSuccess = @"登陆成功";

difference between the two

  • defineSubstitutions are made in the preprocessing stage, and constconstants are used in the compilation stage;
  • defineNo type checking is performed, only replacement is performed. constConstants have data types, and type checking will be performed;
  • defineCannot be debugged, constconstants can be debugged;
  • defineThe defined constants will continue to occupy memory during the running process after replacement, and the constdefined constants are stored in the data segment with only one copy, which is more efficient;
  • defineFunctions can be defined, but constnot.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324859875&siteId=291194637