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
What’s the secret behind Tik Tok’s popularity? Ti...
As the most prominent members of the Arachnida, s...
, , , , , , , For a 2-yuan ticket of the Double C...
On April 25, BYD Dynasty's new mid-size sedan...
London in the late 19th century was a place full ...
In 2023, China officially entered the manned spac...
How long do you spend in the toilet each time? 5 ...
It is an indisputable fact that online traffic is...
Many people shudder when seeing the word "gh...
What is Data Analytics ? Data analysis refers to ...
According to the official website of the State Ad...
If you are working in the education industry, you...
The two most popular words recently are online an...
If you have been using Mac for work and suddenly ...
The Qingming Festival is just a few days away, and...