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
Your ideal reader is probably like this: someone ...
[[240031]] Google has done something bad (again)....
A friend who does e-commerce asked on WeChat: &qu...
In a blink of an eye, Android has become a 10-yea...
Audit expert: Li Weiyang Well-known science write...
Usually after completing a transaction, we can sh...
Editor's note: Today's post is about a ki...
Recently, the US short-selling agency Muddy Water...
END Tadpole five-line score original work, please...
WeChat open platform semantic understanding inter...
Yesterday, Lenovo announced its 2013-2014 fiscal y...
Lv Weitao, Curator at the National Museum of Chin...
On the riverside of the French city of Sens, tube...
Xiao Wu's 21-day high-quality parent-child re...