iOS development: using Const,static,extern correctly

Source: Internet
Author: User
Tags constant modifier variable scope

Objective

This article mainly describes how to use CONST,STATIC,EXTERN3 in the development of keywords, if you like my article, you can pay attention to my microblog: Acridine a Zheng, you can also come to small code brother, understand our iOS training courses. More content will be updated later ...

The difference between the const and the macro (interview question):

Introduction to Const: Previously used string constants, usually smoked macro, but Apple does not recommend us to smoke macro, we recommend the use of const constants.

Compile time: The macro is precompiled (processed before compiling), and the const is the compile phase.

Compile check: macros do not check, do not report compilation errors, just replace, const will compile the check, will report a compilation error.

Benefits of macros: macros can define functions, methods. Const cannot.

The disadvantage of macros: using a large number of macros, easily lead to a long time to compile, each need to be replaced.

Note: Many blogs say that using macros, will consume a lot of memory, I this validation does not generate a lot of memory, the macro is defined as a constant, constants are placed in the constant area, will only generate a memory.

Common constants: a macro

#define XMGACCOUNT @ "Account"

#define Xmguserdefault [Nsuserdefaults Standarduserdefaults]

String constants

Static NSString * Const ACCOUNT = @ ' account ';

-(void) Viewdidload {

[Super Viewdidload];

Preferences Store

Using macros

[Xmguserdefault setvalue:@ "123" forkey:xmgaccount];

Using the const constant

[[Nsuserdefaults Standarduserdefaults] setvalue:@ "123" forkey:account];

}

Ii. the role of const: restriction type

1.const is only used to modify the right variable (basic data variable p, pointer variable *p)

2. Variables modified by the const are read-only.

Const BASIC Use

-(void) Viewdidload {

[Super Viewdidload];

Defining variables

int a = 1;

Allow values to be modified

A = 20;

Const two ways to use

Const: Modifying the base variable p

The two ways are the same, const only modifies the base variable B on the right.

const int b = 20; B: Read-only variables

int const B = 20; B: Read-only variables

Modifying values is not allowed

b = 1;

Const: modifier pointer variable *p, with * variable, is the pointer variable.

Defines a pointer variable that points to the int type, pointing to the address of a

int *p = &a;

int c = 10;

p = &c;

Allows you to modify the address that P points to,

Allows you to modify the value of the P access memory space

*p = 20;

The const modifier is the memory space accessed by the pointer variable, decorated with the right *p1,

Two ways.

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

Two ways.

const int * const P1; *P1: constant P1: constant

int const * Const P1; *P1: constant P1: constant

}

Three, the use of the scenario of Const development:

1. Demand 1: Provide a method, the parameter of this method is the address, which can only read the value through the address, can not modify the value of the address

2. Demand 2: Provide a method, the parameter of this method is the address, which can not modify the address of the parameter.

@implementation Viewcontroller

CONST PUT * Front constraint parameter, indicates *a read only

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

-(void) test: (const int *) A

{

*a = 20;

}

Const-Put * behind constraint parameters that represent a read-only

Can not modify the address of a, can only modify the value of a access

-(void) Test1: (int * const) A

{

int b;

Will complain

A = &b;

*a = 2;

}

-(void) Viewdidload {

[Super Viewdidload];

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

int a = 10;

Requirement 1: Provides a method, the parameter of this method is the address, inside can only read the value through the address, cannot modify the value through the address.

This is the time to use const, which constrains the parameters of the method to read only.

[Self test:&a];

Requirement 2: Provide a method, the parameter of this method is the address, which cannot modify the address of the parameter.

[Self test1:&a];

}

@end

Four, static and extern simple use (to use a thing, first understand its role)

static function:

To modify a local variable:

1. Extend the life cycle of local variables, the end of the program will be destroyed.

2. Local variables generate only one memory, which is initialized only once.

3. Change the scope of local variables.

To decorate a global variable

1. Can only be accessed in this file, modify the scope of the global variable, the life cycle will not change

2. Avoid repeating the definition of global variables

extern function:

is used to get the values of global variables (including global static variables) and cannot be used to define variables

The principle of extern work:

In the current file to find there is no global variables, not found, will go to other files to find.

Global variables: Only one memory, all file shares, used in conjunction with extern.

int a = 20;

Static Cosmetic global variable

static int age = 20;

-(void) test

{

static modifier local variable

static int age = 0;

age++;

NSLog (@ "%d", age);

}

-(void) Viewdidload {

[Super Viewdidload];

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

[Self test];

[Self test];

extern int age;

NSLog (@ "%d", age);

}

I

Joint use of static and const

Static and const actions: Declaring a read-only static variable

Development usage Scenarios: string constants that are frequently used in a file, which can be combined with static and const

Common static modifies global variables in development, changing scope only

Why change the global variable scope to prevent repeated declarations of global variables.

Global variables declared in development, some do not want external changes, only allowed to read.

Like a basic data type that doesn't want someone else to change.

Declares a static global read-only constant

static const int a = 20;

The role of Staic and const unions: Declaring a static global read-only constant

The Staic and const common use scenarios in iOS are used to replace macros, and to define a constant-use string constants as static global read-only variables.

Development often get key modify value, so use the Const modifier key, indicating key read-only, not allowed to modify.

Static NSString * Const KEY = @ "name";

If the const modifier *key1, which means that *key1 is read-only, key1 can still change.

static NSString Const *KEY1 = @ "name";

Vi. joint use of extern and const

Use scenarios in development: the same string constants that are frequently used in multiple files, and you can use extern and const combinations.

Reason:

Static and const combinations: Each file needs to define a static global variable.

extern and const combination: Just define a global variable, multiple file shares.

Global constants Regular writing: in the development of easy to manage all global variables, usually engage in a globeconst file, which specifically define global variables, unified management, otherwise the project document is more difficult to find.

GlobeConst.h

/******************************* Home ****************************/

extern NSString * Const NAMEKEY = @ "name";

/******************************* Home ****************************/

Globeconst.m

#import

/******************************* Home ****************************/

NSString * Const NAMEKEY = @ "name";

/******************************* Home ****************************/

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.