Const, static, extern introduction, constextern

Source: Internet
Author: User

Const, static, extern introduction, constextern
Const, static, extern Introduction 1. Difference Between const and macro:

  • Const Introduction: the commonly used string constants are generally extracted into macros, but apple does not recommend that we extract them into macros. We recommend that you use const constants.
    • Execution time: macros are pre-compiled (processed before compilation), and const is the compilation stage.
    • Compile check: If the macro does not check, no compilation error will be reported, but the const will check the compilation and report a compilation error.
    • The benefit of macros: macros can define some functions, methods, and const cannot.
    • Disadvantages of macros: using a large number of macros can easily lead to a long Compilation Time, and each time it needs to be replaced again.

Note: many blogs say that using macros will consume a lot of memory. I will not generate much memory here for verification. Macros define constants and constants are placed in the constant area, only one copy of memory is generated.

 

1 // common constants: extract macro 2 # define YGAccount @ "account" 3 4 # define YGUserDefault [NSUserDefaults standardUserDefaults] 5 6 // String constant 7 8 static NSString * const account = @ "account "; 9 10-(void) viewDidLoad 11 {12 [super viewDidLoad]; 13 14 // preference storage 15 // use macro 16 17 [YGUserDefault setValue: @ "Sunshine" forKey: YGAccount]; 18 19 // use the const constant 20 [[NSUserDefaults standardUserDefaults] setValue @ "Sunshine" forKey: account]; 21 22}
Ii. const role: restriction type
  • 1. const is only used to modify the variable on the right (Basic Data Variable p, pointer variable * p)
  • 2. The variable modified by const is read-only.

Basic use of const:

1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 5 // define the variable 6 int a = 1; 7 8 // allow value 9 a = 20; 10 11 // const two usage 12 // const: Modify the basic variable p13 // these two statements are the same, const only modifies the basic variable b14 15 const int B = 20; // B: Read-Only variable 16 int const B = 20; // B: the read-only variable 17 18 // The value 19 B = 1 cannot be modified; 20 21 // const: modifies the pointer variable * p. The variable with * is the pointer variable. 22 // define a pointer variable pointing to the int type, pointing to the address of a 23 24 int * p = & a; 25 26 int c = 10; 27 28 p = & c; 29 30 // allow modifying the address pointed to by p, 31 // allow modifying the value of p to access the memory space 32 33 * p = 20; 34 35 // const modifies the memory space accessed by the pointer variable, and modifies * p1, 36 on the right. // the two methods are the same: 37 38 const int * p1; // * p1: constant p1: Variable 39 int const * p1; // * p1: constant p1: Variable 40 41 // const modifier pointer variable p142 int * const p1; // * p1: variable p1: constant 43 44 // first const modifier * p1 second const modifier p145 // both methods are the same 46 47 const int * const p1; // * p1: constant p1: constant 48 49 int const * const p1; // * p1: constant p1: constant 50 51}

 

Iii. Use Cases in const development:
  • Requirement 1: provide a method. The parameter of this method is the address, which can only be read through the address and cannot be modified through the address.
  • Requirement 2: provide a method. The parameter of this method is the address, which cannot be modified.
1 @ implementation ViewController 2 3 // put the preceding constraint parameter in const, which indicates * a read-only 4 // only address a can be modified, do not use a to modify the access memory space 5 6-(void) test :( const int *) a 7 {8 9 // * a = 20; 10 11} 12 13 // put the constraint parameter * after the const, indicating that a is read-only 14 // the address of a cannot be modified, and the Access value of a is only 15 16-(void) test1 :( int * const) a17 {18 19 int B; 20 // 21 a = & B; 22 23 * a = 2; 24 25} 26 27 28-(void) viewDidLoad 29 {30 [super viewDidLoad]; 31
1 // requirement 1: provide a method. The parameter of this method is the address, which can only be read through the address, but cannot be modified through the address. 2 3 // at this time, you need to use const. the parameter of the constraint method is read-only, 4 5 [self test: & a]; 6 7 8 // requirement 2: provide a method, the parameter of this method is the address, which cannot be modified. 9 [self test1: & a];
33 }
34 @end

 

Iv. simple use of static and extern

  • Static function:

    • Modify local variables:
      • Extends the declaration period of local variables before the program ends.
      • Local variables only generate one copy of memory and will only be initialized once.
      • Change the scope of a local variable.
        • Modify global variables

 

      • It can only be accessed in this file. The scope of the global variable is modified and the lifecycle is not changed.
      • Avoid defining global variables repeatedly.
  • Function of extern:

    • It is only used to obtain the value of a global variable (including a Global static variable) and cannot be used to define a variable.
  • How extern works:

    • Check whether there are global variables in the current file. If no global variables are found, go back to other files.

 

1 // global variable: Only one memory, all files are shared and used together with extern. 2 int a = 20; 3 4 // static modifier global variable 5 static int age = 20; 6 7-(void) test 8 {9 // static modifier local variable 10 static int age = 0; 11 age ++; 12 NSLog (@ "% d", age ); 13} 14 15-(void) viewDidLoad 16 {17 [super viewDidLoad]; 18 19 [self test]; 20 [self test]; 21 22 extern int age; 23 NSLog (@ "% d", age); 24}

 

V. Joint Use of static and const

  • Static and const: declares a read-only static variable.
  • Development and application scenarios: string constants often used in a file, which can be combined with static and const.
// Global variables are often modified using static in development, and only the scope is changed. // Why do we need to change the global variable scope to prevent repeated declarations of global variables. // The global variables declared during development. Some variables can only be read without external changes. // For example, you do not want to change a basic data type. // Declare a static global read-only constant.

Static const int a = 20;

// Combination of static and const: declares a static global read-only constant.

// Common Use Cases of static and const in iOS are used to replace macros and define a commonly used String constant as a static global read-only variable.

// The key Modification value is often obtained during development. Therefore, using the const to modify the key indicates that the key is read-only and cannot be modified.

Static NSString * const key = @ "name ";

// If the const modifier * key1 indicates read-only, key1 can still be changed.
  
Static NSString const * key1 = @ "name ";

 

6. Joint Use of extern and const
  • Use Cases in development: the same character constant is often used in multiple files and can be combined with extern and const.
  • Cause:
    • Static and const combination: a static global variable must be defined in each file.
    • Combination of extern and const: you only need to define a global variable and share multiple files.
  • Regular Expression of global constants: it is easy to manage all the global variables in development. Generally, a GlobeConst file is created, which defines global variables and manages them in a unified manner. Otherwise, it is hard to find a project file that is too many.
  • GlobeConst. h
********* * ******************/Extern NSString * const nameKey = @ "name "; ********* ********************/
  • GlobeConst. m
# Import <Foundation/Foundation. h> ******* * ********************/NSString * const nameKey = @ "name "; ********* ********************/

 

    

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.