IOS keyword const, static, extern details:
1. Preface
When reading other people's code (some excellent source code) you can always find some common keywords. As you accumulate programming experience, most of them still know what it means, but they are still a little vague in concept and specific usage. Therefore, I specially compiled and summarized the following three more common keywords - const/static/extern.
2. The definition and usage of the keyword const/static/extern
1、const
This word translates into Chinese to mean "constant". In the program, we know that the value of "constant" cannot be changed and fixed. So the function of the const keyword is about to come out:
(1) Const is used to modify the basic variable or pointer variable on the right
(2) The modified variable is read-only and cannot be modified
Here is the simplest example:
//Declare a variable a of type int, the variable initialization value is 10, and there is a const keyword modification to the left of the variable aint const a = 10; //Because the variable a is modified by const, it becomes read-only and cannot be modified and assigned, so the following line of code is wronga = 20;//Error code //The first code above is equivalent to this code, and they both modify the variable a to make it read onlyconst int a = 10;
Let’s take a look at another set of exercises. After this set of exercises is completed, I believe you will fully understand the usage of const:
Indicate whether *p and p in the following four lines of code are read-only or variables
int const *p // *p read-only;p variable int * const p // *p variable; p read only const int * const p // Both p and *p are read only int const * const p // Both p and *p are read only
Note: When judging whether p and p are read-only or variables, the key is to see who is ahead of const. If it is only before p, then p is read only, p is still a variable; if it is before p, then p is read only, p variable.
Common usage of const:
//Define a global read-only variableNSString * const Kname = @"appkey"; //After static modification, this global variable can only be accessed by this filestatic NSString *const Key = @"hddjj”;
2、static
The word translated into Chinese means "static". Literally, Guan cannot be related to his role. Let’s look at his role first:
(1) Modify local variables
Ensure that local variables are only initialized once, and there is always only one memory during the operation of the program. The life cycle is similar to global variables, but the scope remains unchanged. How do you understand this sentence? Let's explain it with code examples.
Build any project and listen to the click event method of the controller view on a controller class:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //Declare a local variable i int i = 0; //Every time you click the view to get this method, let i increase it by itself i ++; //Print results NSLog(@"i=%d",i); }
The output log is as follows:
2016-10-26 14:58:48.290 fff[2760:170260] i=1 2016-10-26 14:58:49.044 fff[2760:170260] i=1 2016-10-26 14:58:49.200 fff[2760:170260] i=1 ....
From the output log, we can see that i always equals 1, which is expected, because every time I click to enter this method, a brand new variable i = 0 will be reinitialized, and the value will become 1 after adding it once, and then the result will be printed as 1. After this method is released, the local variable i will be released and recycled. So the result of each printout is 1.
But let's look at the situation where the local variable i is modified by the keyword static:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //Declare a local variable i static int i = 0; //Every time you click the view to get this method, let i increase it by itself i ++; //Print results NSLog(@"i=%d",i); }
The output log is as follows:
2016-10-26 15:07:34.276 fff[2817:175155] i=1 2016-10-26 15:07:35.347 fff[2817:175155] i=2 2016-10-26 15:07:35.761 fff[2817:175155] i=3 2016-10-26 15:07:36.057 fff[2817:175155] i=4 2016-10-26 15:07:36.415 fff[2817:175155] i=5 ....
You can see in the above log that the value of i is constantly increasing. What? It is initialized and assigned to 0 every time it goes in, how can it be accumulated? This is the function of local variables modified by the keyword static, so that local variables are only initialized once, with a piece of memory, and the life cycle is similar to global variables, but the scope remains unchanged.
(2) Modify global variables
Make the scope of the global variable limited to the current file, that is, the global variable can only be accessed within the current file.
The global variable declared in one file in iOS can also be accessed by other files, but I don’t want other files to access it. At this time, I can modify it with static. A typical example is a singleton created using GCD one-time function. Global variables are basically modified by static.
Below is a simple profit created by a GCD function
@implementation LoginTool
//static modify global variables, making external files inaccessible
static LoginTool *_sharedManager = nil;
+ (LoginTool *)sharedManager { static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ _sharedManager = [[self alloc] init]; }); return _sharedManager; }
(3) Modification function
When modifying a function statically, the modified function is called a static function, making it impossible for external files to access this function, and only this file can access it. This is rarely used in the development of the OC language, but the C language can see some shadows, so I will not discuss it in detail.
3、extern
The word translates to "outside, external". As the name implies, its function is to declare external global variables. It is important to note that extern can only be declared and cannot be used for implementation.
In development, we usually draw a class separately to manage some global variables or constants. Let’s take a look at a relatively high-quality approach:
We can declare some global constants in the .h file
//Declare some global constants extern NSString * const name; extern NSInteger const count;
Then implement it in the .m file
#import <Foundation/> //accomplish NSString * const name = @"Wang Wu"; NSInteger const count = 3;
In this way, as long as the header file is imported, the defined variables or constants can be used globally.
3. Conclusion
Of course, there are many common keywords that will be added later when you see more valuable (somewhat difficult and common). This article ends here. If there are any omissions, please correct me!
Thank you for reading, I hope it can help you, thank you for your support!