Summary of the const static extern keyword in IOS

Source: Internet
Author: User
Tags gcd

In looking at some of the code written by the master, can always see our small white usually do not use the key words, once, two times, three times, can not always be shady, and now summarize the daily development of commonly used key words function:

Definition and usage of keyword Const/static/extern

1. Const

The word translated into Chinese is the meaning of "constant". In the program we know that the value of the constant is immutable and fixed. So the Const keyword works:

(1) const is used to modify the underlying variable or pointer variable on the right

(2) The modified variable is read-only and cannot be modified

Here's a simple example:

// declares a variable of type int with a initialization value of 10 and a is decorated        with the const keyword int Const Ten ;         // because A is modified by const, it becomes read-only and will be error-corrected. //         a=; // (This code will be an error        ) // The above code is equivalent        to // const int a = ten;        NSLog (@ "Hello, world! ");

Take a look at the summary below

int  Const  *p   //  *p read-only;p variable int  const  P  //  *p variable; P read-only const  int   const//P and *p are read-only int   const  const  P   //P and *p are read-only

Note: The key to judging whether P and p are read-only or variable is to see who is in front of the const. If it is only in front of P, then p is read-only *p or variable, and if before *p, p is the variable *p is read-only.

2.static

The word translated into Chinese is "static" meaning, only literally understand that there is no way to spy on his role. Let's look at his role directly:

(1) Modifying local variables

Ensure that local variables are always initialized only once, and that there is always only one memory in the program running, and the life cycle is similar to global variables. But the scope does not change. How to understand this sentence, the last example is clear.

Build a project and listen to the Controller view's Click event method on a controller class:

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event{    //  Declares a local variable i    int0;    // each time you click on the view to this method, let I increment    i + +;    // Print Results    NSLog (@ "i=%d", I);}

The output logs are as follows:

 .-Ten- -  -: -:48.290fff[2760:170260] I=1 .-Ten- -  -: -:49.044fff[2760:170260] I=1 .-Ten- -  -: -:49.200fff[2760:170260] I=1....

From the output log we can see that I has been equal to 1, which is also expected, because each click into this method will reinitialize a new variable i = 0, Gaga once the value becomes 1, and then print the result is 1, After this method, the local variable i is released for recycling. So every time you print out the results are 1.

But let's look at the case where the local variable I was modified by the keyword static:

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event{    //  Declares a local variable i  static  int0;    // each time you click on the view to this method, let I increment    i + +;    // Print Results    NSLog (@ "i=%d", I);}

The output logs are as follows:

 .-Ten- -  the: -:34.276fff[2817:175155] I=1 .-Ten- -  the: -:35.347fff[2817:175155] I=2 .-Ten- -  the: -:35.761fff[2817:175155] I=3 .-Ten- -  the: -:36.057fff[2817:175155] I=4 .-Ten- -  the: -:36.415fff[2817:175155] I=5....

I can see from the above log that the value of I has been self-increasing, what? Didn't he get initialized every time he went in? is the value of 0 assigned? How does it accumulate? This is the function of the keyword static modifier local variable, so that the local variable is always initialized only once, a portion of memory, the life cycle is similar to the global variable, but the scope is unchanged.

(2) Modifying global variables

The scope of the global variable is limited to the inside of the current file, that is, the global variable is accessible within the current file.

The global variables declared in a file in iOS, other files in the project can be accessed, but I do not want to let other files access, this can be used to decorate it with the static keyword, the more typical use is GCD one-time function created by the Singleton, the global variable will basically use static decoration.

Here is a singleton created using the GCD disposable function

@implementation Logintool // static modifies global variables so that external files cannot be accessed Static Logintool *_sharedmanager = nil; + (Logintool *) sharedmanager {      static  dispatch_once_t oncepredicate;      Dispatch_once (&oncepredicate, ^{        = [[Self alloc] init];    });       return _sharedmanager;}

(3) Modifier function

static modifier function, the modified function becomes a static function, so that the external file cannot access the function, only this file can be accessed.

Three key words: extern

This word translates to "outside", "external". As the name implies, his role is to declare external local variables. It is important to note that extern can only be declared and cannot be used for implementation.

In development we often pull out a single class to manage some global variables or constants. Let's take a look at the high-force aligning approach:

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

// declare some global constants extern Const name; extern Const Count;

And then in. m to implement

Const @" Harry "  const3;

In this way, you can use the defined variables or constants globally as long as you import the header file.

Supplement the difference between using the Const keyword and the macro definition

Generally we macro the usual string variables, but Apple does not recommend us to smoke macro, we recommend using const constants.

Compile time: The macro is precompiled (pre-processing before compilation) const is the compile phase.

Compile check: macros do not check, do not compile error, just replace; const compiles the check and reports compilation errors.

Benefits of macros: macros can define some function methods;

Macro disadvantages: Using a large number of macros is easy to compile for a long time and needs to be replaced every time.

Summary of the const static extern keyword in IOS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.