IOS development: Correct Use of const, static, and extern

Source: Internet
Author: User

IOS development: Correct Use of const, static, and extern

Preface

This article mainly describes how to use the const, static, and extern3 keywords in development. If you like my article, you can follow my weibo blog: I have a problem, or you can come to xiaocode, learn about our iOS training courses. More content will be updated later...

I. Difference Between const and macro (interview questions ):

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.

Compile time: macros are pre-compiled (processed before compilation), and const is the compilation phase.

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 and methods. 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 for this verification. Macros define constants and constants are placed in the constant area, only one copy of memory is generated.

// Common constants: extract them into macros

# Define XMGAccount @ "account"

# Define XMGUserDefault [NSUserDefaults standardUserDefaults]

// String constant

Static NSString * const account = @ "account ";

-(Void) viewDidLoad {

[Super viewDidLoad];

// Store preference settings

// Use macros

[XMGUserDefault setValue: @ "123" forKey: XMGAccount];

// Use a const constant

[[NSUserDefaults standardUserDefaults] setValue: @ "123" forKey: account];

}

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

-(Void) viewDidLoad {

[Super viewDidLoad];

// Define variables

Int a = 1;

// Allow value Modification

A = 20;

// Const usage

// Const: modifies the basic Variable p.

// The two methods are the same. const only modifies the basic variable B on the right.

Const int B = 20; // B: Read-Only variable

Int const B = 20; // B: Read-Only variable

// The value cannot be modified.

B = 1;

// Const: modifies the pointer variable * p. The variable with * is the pointer variable.

// Define a pointer variable pointing to the int type, pointing to the address of

Int * p = &;

Int c = 10;

P = & c;

// You can modify the address pointed to by p,

// Modify the value of p allowed to access the memory space

* P = 20;

// Const modifies the memory space accessed by the pointer variable. It modifies the right side of * p1,

// The two methods are the same

Const int * p1; // * p1: constant p1: Variable

Int const * p1; // * p1: constant p1: Variable

// Const modifier pointer variable p1

Int * const p1; // * p1: Variable p1: Constant

// The first const modifier * p1 the second const modifier p1

// The two methods are the same

Const int * const p1; // * p1: constant p1: Constant

Int const * const p1; // * p1: constant p1: Constant

}

Iii. Use Cases in const development:

1. 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.

2. requirement 2: provide a method. The parameter of this method is the address, which cannot be changed.

@ Implementation ViewController

// Put the preceding constraint parameter in const, indicating that * a is read-only.

// Only address a can be modified, and the access memory space cannot be modified through address

-(Void) test :( const int *)

{

// * A = 20;

}

// Put the constraint parameter after const *, indicating that a is read-only

// The address of a cannot be modified. Only the access value of a can be modified.

-(Void) test1 :( int * const)

{

Int B;

// An error is reported.

A = & B;

* A = 2;

}

-(Void) viewDidLoad {

[Super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

Int a = 10;

// 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.

// At this time, you need to use const. The parameters of the constraint method are read-only.

[Self test: & a];

// Requirement 2: provide a method. The parameter of this method is the address, which cannot be changed.

[Self test1: & a];

}

@ End

4. simple use of static and extern (to use one thing, first understand its role)

Static function:

Modify local variables:

1. extend the life cycle of local variables before the program is destroyed.

2. Local variables only generate one copy of memory and only initialize it once.

3. Change the scope of local variables.

Modify global variables

1. It can only be accessed in this file. modifying the scope of global variables will not change the lifecycle.

2. Avoid repeated definitions of global variables

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:

If no global variable is found in the current file, other files will be searched.

// Global variable: Only one memory, all files are shared and used together with extern.

Int a = 20;

// Modify global variables in static mode

Static int age = 20;

-(Void) test

{

// Modify local variables in static mode

Static int age = 0;

Age ++;

NSLog (@ "% d", age );

}

-(Void) viewDidLoad {

[Super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[Self test];

[Self test];

Extern int age;

NSLog (@ "% d", age );

}

I

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 in static mode during development. 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;

// Joint role of staic and const: declare a static global read-only constant

// Common Use Cases of staic 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 that * key1 is read-only, key1 can still be changed.

Static NSString const * key1 = @ "name ";

6. Joint Use of extern and const

Application scenarios in development: the same String 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 difficult to find many project files.

GlobeConst. h

******* *********************/

Extern NSString * const nameKey = @ "name ";

******* *********************/

GlobeConst. m

# Import

******* *********************/

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.