IOS keyword const, static, extern detailed _ios

Source: Internet
Author: User
Tags gcd modifier

IOS keyword const, static, extern detail:

First, the preface

Reading other people's code (some excellent source) can always find some common keywords, with the accumulation of programming experience most still know what it means, but in terms of conceptual and specific usage is still somewhat vague, so deliberately collated the next three more common keywords-- Const/static/extern.

Ii. the definition and usage of the key word Const/static/extern

1. Const

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

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

Here's the simplest example:

Declares a variable a of type int, the variable initialization value is 10, and the variable A has a const keyword on the left modifier
int const A = ten;
Because variable A is modified by a const, it becomes read-only and cannot be modified, so the following line of code is the wrong
a = 20;//error code

//The first code above is equivalent to this code, all modified variable A to read the
const int  a = 10;

Let's look at a set of exercises, and after this set of exercises, I believe you fully understand the use of the const:

Indicate whether *p and p are read-only or variable in the following four lines of code

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 are read only

Note: To determine whether p and p are read-only or variable, the key is to see who is in front of the const. If it is only in front of P, then p is read only, p is a variable, and if it precedes p, then p is read only, p variable.

Common usage of const:

Defines a global read-only variable
nsstring * Const KNAME = @ "Appkey";

After static decoration, this global variable can only access the
static nsstring *const Key = @ "HDDJJ";

2. Static

The word translated into Chinese is "static" meaning. The literal understanding does not relate to his function, so let's look at his role directly first:

(1) Modifying local variables

To ensure that local variables are always initialized once, in the process of running the program will always have only one memory, the lifecycle is similar to global variables, but the scope does not change. How do you understand this sentence? Let's take a code example to explain it.

Randomly build a project to monitor the Click event method for the controller view on a controller class:

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
  //Declare a local variable i
  int i = 0;
  //Every Click View comes to this method to let I self-add
  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, this is also expected, because each click to enter this method will reinitialize a new variable i = 0, gaga after a value of 1, and then print out the result is 1, after this method, the local variable I was released to recycle. So every time you print out the results are 1.

But let's look at the case where the local variable i is decorated with the keyword static:

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
  //Declare a local variable i
 static int i = 0;
  Every time you click on the view to this method let I add
  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 ...
.

The above log can see that the value of I has been increasing since. What, it is not every time in the initialization of the assignment is 0, how can add it. This is the role of the local variable of the keyword static modifier, so that the local variable is always initialized once, a memory, the lifecycle has been similar to the global variable, but the scope is unchanged.

(2) Modifying global variables

The scope of the global variable is limited to the current file, that is, the current file cannot access the global variable.

iOS in a file declaration of the global variables, the project other files are also accessible, but I do not want to make other file access, then you can modify it with static, more typical is the use of GCD one-time function created by the single example, the global variables are basically static modification.

The following is a GCD function creation simple

@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, ^{
    _sharedmanager = [[Self alloc] init];
  });
  return _sharedmanager;
}

(3) Modifier function

When the static modifier functions, the modified function is called a static function, which makes the external file inaccessible to this function and is accessible only to this file. This is rarely used in OC language development, C language can see some shadow, so do not discuss in detail.

3, extern

The word translated is "outside, outside." As the name suggests, its role is to declare external global variables. This requires special attention that extern can only declare and cannot be used for implementation.

In development, we usually draw a separate class to manage some global variables or constants, and here's a look at the higher-aligning approach:

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

Declares some global constants

extern nsstring * Const name;

extern Nsinteger const count;

and implement it in the. m file.

#import <Foundation/Foundation.h>

//Implementation

NSString * Const NAME = @ "Harry";

Nsinteger Const COUNT = 3;

This allows you to use a defined variable or constant globally, as long as you import the header file.

Third, concluding remarks

Of course, there are a lot of common keywords, and later see more valuable (somewhat difficult and more common) will continue to add. The end of this article, if there are omissions, look please correct me!

Thanks for reading, hope to help everyone, thank you for your support to the cloud-dwelling community!

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.