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
Shaya County in Aksu Prefecture, Xinjiang, is loc...
Brand loyalty is often mentioned in daily communi...
How to create your own influencer to sell product...
Recently, the first case of illegal P2P fund-rais...
The world is so big that there are all kinds of s...
Inspired by the recent criticism of traffic fraud...
Many netizens have encountered some scams, such a...
Playing a joke on April Fool's Day has almost...
Alipay can now also use digital RMB. You can choo...
In recent exchanges with some readers, I have dis...
On October 18, every time Android releases a new ...
As a preliminary note, with the listing of Intern...
Nowadays, the ratio of men to women is unbalanced...
New MIT technology helps illuminate the inner wor...
Audit expert: Lu Bin Deputy Chief Physician and A...