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 const
anddefine(宏定义)
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 world
withworld
two 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
const
the 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
*scottDidLoginSuccess
cannot be modified,scottDidLoginSuccess
can be modified*scottDidLoginSuccess
Can not be modified,scottDidLoginSuccess
can be modified, which is the same as the first casescottDidLoginSuccess
Can not be modified,*scottDidLoginSuccess
can 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
define
Substitutions are made in the preprocessing stage, andconst
constants are used in the compilation stage;define
No type checking is performed, only replacement is performed.const
Constants have data types, and type checking will be performed;define
Cannot be debugged,const
constants can be debugged;define
The defined constants will continue to occupy memory during the running process after replacement, and theconst
defined constants are stored in the data segment with only one copy, which is more efficient;define
Functions can be defined, butconst
not.