Preface This article mainly introduces how to use the three keywords const, static, and extern in development. If you like my article, you can follow my Weibo: 吖了个郑, or you can come to Xiaoma Ge to learn about our iOS training courses. More content will be updated later. . . 1. The difference between const and macro (interview question): Introduction to const: String constants that were commonly used before were generally extracted into macros, but Apple does not recommend that we extract macros and recommends that we use const constants. Compile time: macros are precompiled (processed before compilation), and const is at the compilation stage. Compilation check: Macros do not do any checking and will not report compilation errors, they are just replaced. Const will do compilation checking and will report compilation errors. Advantages of macros: Macros can define some functions and methods, but const cannot. Disadvantages of macros: Using a large number of macros can easily cause long compilation time and they need to be replaced every time. Note: Many blogs say that using macros will consume a lot of memory. My verification does not generate a lot of memory. Macros define constants, which are placed in the constant area and only generate one copy of memory.
2. The role of const: limiting types 1.const is only used to modify the variable on the right (basic data variable p, pointer variable *p) 2. Variables modified by const are read-only. Basic use of const
3. Usage scenarios in const development: 1. Requirement 1: Provide a method whose parameter is an address. The value can only be read through the address, but not modified through the address. 2. Requirement 2: Provide a method whose parameter is an address, and the address of the parameter cannot be modified. @implementation ViewController
4. Simple use of static and extern (to use something, first understand its function) static effect: Modify local variables: 1. Extend the life cycle of local variables so that they will be destroyed only when the program ends. 2. Local variables will only generate one copy of memory and will only be initialized once. 3. Change the scope of local variables. Modifying global variables 1. Only accessible in this file, modifying the scope of global variables, the life cycle will not change 2. Avoid redefining global variables Extern function: It is only used to obtain the value of global variables (including global static variables) and cannot be used to define variables. How extern works: First, search for global variables in the current file. If not found, search other files.
5. Use static and const together Static and const functions: declare a read-only static variable Development usage scenario: string constants frequently used in a file can be combined with static and const
static NSString const *key1 = @"name"; 6. Use extern and const together Usage scenarios in development: The same string constant that is often used in multiple files can be combined with extern and const. reason: Combination of static and const: A static global variable needs to be defined in each file. Combination of extern and const: only one global variable needs to be defined and multiple files can share it. Formal writing of global constants: It is convenient to manage all global variables during development. Usually, a GlobeConst file is created to define global variables and manage them in a unified way. Otherwise, it will be difficult to find many project files.
|
>>: Android slide close activity
[[142899]] Microsoft is currently developing a la...
In July 2008, Apple officially launched the App S...
It is said that if the underwear is well chosen, ...
A piece of news about Windows 10 these days has ma...
The love of beauty has existed since ancient times...
On the morning of June 4, D2809 train was hit by ...
For most consumers, whether the camera function i...
Many people have written to me and asked me, what...
Author: Song Qin, deputy chief physician of the D...
This is my first attempt at writing a competitive...
Learn to master: Learn how to operate Alibaba Int...
Depression, a global public health problem, affec...
01Settlement and Binding 1. You can register a Ku...
Kuaishou and Douyin, as the two representative pl...