Detailed explanation of IOS keywords const static and extern

  • 2021-07-01 08:19:10
  • OfStack

IOS keywords const, static, extern details:

1. Preface

Reading other people's code (1 some excellent source code) can always find 1 some common keywords. With the accumulation of programming experience, most of them still know what it means, but they are still somewhat vague in concept and specific usage. Therefore, the next 3 common keywords-const/static/extern are specially sorted out and summarized.

2. Definition and usage of the keyword const/static/extern

1. const

This word means "constant" when translated into Chinese. In the program, we know that the value of "constant" cannot be changed and fixed. Therefore, the role of const keyword is vividly portrayed:

(1) const is used to modify the base variable or pointer variable on the right
(2) Modified variables are read-only and cannot be modified

Here's the simplest example:


// Declaration 1 A int Variable of type a The initialization value of the variable is 10 And the variable a On the left 1 A const Keyword modification 
int const a = 10;
// Because variables a Be const Modified, it becomes read-only and cannot be modified and assigned, so the following line of code is wrong 
a = 20;// Error code 

// Above 1 Sentence code and this sentence code are equivalent, and they are all modifiers a Make it read-only 
const int  a = 10;

Let's 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 are read-only or variables in the following four lines of code, respectively


int const *p  // *p Read-only  ;p Variable 

int * const p // *p Variable  ; p Read-only 

const int  * const p //p And *p All read-only 

int const * const p  //p And *p All read-only 

Note: The key to judging whether p and p are read-only or variable is to see who comes ahead of const. If it is only before p, then p is read-only and p is still a variable; If it comes before p, then p is read-only and p variable.

Common uses of const:


// Definition 1 Global read-only variables 
NSString * const Kname = @"appkey";

//static This global variable can only be accessed by this file after modification 
static NSString *const Key = @"hddjj " ;

2. static

This word means "static" when translated into Chinese. Literally, it really can't be related to his role. Let's look at his role directly:

(1) Modify local variables

Ensure that local variables are always only initialized once, and only one memory is always available during the running of the program. The life cycle is similar to that of global variables, but the scope remains unchanged. How do you understand this sentence? Let's use code examples to explain.

Build a project randomly and listen to the click event method of controller view on a controller class:


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  // Declaration 1 Local variables i
  int i = 0;
  // Every click view When you come to this method, let i Self-increasing 
  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 i1 is equal to 1, which is also expected, because every time you click to enter this method, a brand-new variable i = 0 will be reinitialized, and the value will become 1 after adding once, and then the printed result will be 1. After this method, the local variable i will be released and recycled. So the result printed out every time is 1.

But let's look at the local variable i modified by the keyword static:


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  // Declaration 1 Local variables i
 static int i = 0;
  // Every click view When you come to this method, let i Self-increasing 
  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
....

In the above log, you can see that the value of i is increasing by itself. What, isn't it initialized and assigned to 0 every time it goes in? How can it be accumulated? This is the role of local variables modified by keyword static, so that local variables will only be initialized once and have one memory forever. The life cycle is similar to global variables, but the scope remains unchanged.

(2) Decorate global variables

The scope of a global variable is limited to the inside of the current file, that is, the global variable can only be accessed inside the current file.

In iOS, the global variable declared in one file can be accessed by other files of the project, but I don't want other files to access, so I can modify it with static. Typical is the singleton created by using GCD1 linear function, and the global variable will basically be modified with static.

The following is a simple benefit created by an GCD1 function

@implementation LoginTool

//static Decorate global variables to make external files inaccessible
static LoginTool *_sharedManager = nil;


+ (LoginTool *)sharedManager {
   static dispatch_once_t oncePredicate;
  dispatch_once(&oncePredicate, ^{
    _sharedManager = [[self alloc] init];
  });
  return _sharedManager;
}

(3) Modifying function

When static modifies a function, the modified function is called a static function, so that the function cannot be accessed by external files, but only this file. This in oc language development is rarely used, c language is able to see some shadows, so do not discuss in detail.

3. extern

This word translates as "outside, outside". As the name implies, its function is to declare external global variables. It should be noted here that extern can only be declared and cannot be used for implementation.

In the development, we usually take a separate class to manage some global variables or constants. Let's take a look at one method with higher pressure:

We can declare some global constants in the. h file extern


// Declaration 1 Some global constants 

extern NSString * const name;

extern NSInteger const count;

Then implement it in the. m file


#import <Foundation/Foundation.h>

// Realization 

NSString * const name = @" Wang 5";

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. Concluding remarks

Of course, there are many common keywords, which will continue to be added after seeing more valuable ones (a little difficult and common ones). This is the end of this article. If there are any omissions, please correct me!


Related articles: