About iOS Const,static,extern

Source: Internet
Author: User

Const,static,extern Introduction

The difference between a const and a macro:

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

? compile time: The macro is precompiled (before compilation), and 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: the use of a large number of macros, easy to compile for a long time, each need to replace.

• Note: Many blogs say that using macros will consume a lot of memory, I do not generate a lot of memory, macros are defined as constants, constants are placed in the constant area, only a portion of memory is generated.

Common constants: a macro

#define CJACCOUNT @ "Account"

#define Cjuserdefault [Nsuserdefaults Standarduserdefaults]

String constants

Static NSString * Const ACCOUNT = @ ' account ';

-(void) Viewdidload {

[Super Viewdidload];

Preferences Storage

Using macros

[Cjuserdefault setvalue:@ "123" forkey:cjaccount];

Using const constants

[[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. Const-Modified variables are read-only.

• Const BASIC Use

-(void) Viewdidload {

[Super Viewdidload];

Defining variables

int a = 1;

Allow modification of values

A = 20;

Two usages of const

Const: Modifier basic Variable P

The two formulations are the same, and the const only modifies the basic 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, variable with *, is 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 modifying the value of p to access memory space

*p = 20;

The const modifier pointer variable accesses the memory space, decorated with the right *p1,

Both 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

Both ways

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

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

}

Third, the use of the const development scenario:

• 1. When a method parameter is read-only

• 2. Define read-only global variables

@implementation Viewcontroller

To define a read-only global constant

NSString * Const STR = @ "123";

When a method is a parameter, read-only.

-(void) Test: (NSString * const) name

{

}

Pointer is read-only and cannot be modified by pointer

-(void) Test1: (int const *) a{

*a = 10;

}

Basic data type read-only

-(void) TEST2: (int const) a{

}

@end

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

:

? To Modify a local variable:

1. Extend the life cycle of a local variable, and the program ends before it is destroyed.

2. Local variables generate only one copy of memory and are initialized only once.

? Modifying global variables

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

:

? a value that is used to get global variables (including global static variables) and cannot be used to define variables

:

? first in the current file to find there are no global variables, not found, will go to other files to find.

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

int a = 20;

static modifier global variable

static int age = 20;

-(void) test

{

static modifier local variable

static int age = 0;

age++;

NSLog (@ "%d", age);

}

-(void) Viewdidload {

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

[Self test];

[Self test];

extern int age;

NSLog (@ "%d", age);

}

I

Five, static and const combined use

• static and const action: Declaring a read-only static variable

• Development Usage Scenarios: string constants that are frequently used in a file and can be combined with static and const

Static modified global variables used in development, only changing scope

Why change the scope of global variables to prevent duplicate declarations of global variables.

The global variables declared in development, some do not want outside changes, only allow reading.

Like a basic data type that you don't want others 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

Staic and const common usage scenarios in iOS are used instead of macros to define a frequently used string constant as a static global read-only variable.

The key modification value is often obtained in development, so the Const modifier key means that key is read-only and no modification is allowed.

Static NSString * Const KEY = @ "name";

If const modifies *key1, which means *key1 is read-only, Key1 can still be changed.

static NSString Const *KEY1 = @ "name";

Vi. joint use of extern and const

• use scenarios in development: the same string constants that are used frequently in multiple files can be combined with extern and Const.

• Reason:

? Static and const combinations: a static global variable needs to be defined in each file.

? extern and const combination: You only need to define a global variable, multiple file shares.

• normal notation of global constants: Easy to manage all the global variables in development, usually make a globeconst file, which specifically defines global variables, unified management, or project files more difficult to find.

• GlobeConst.h

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

extern NSString * Const NAMEKEY = @ "name";

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

• GLOBECONST.M

#import <Foundation/Foundation.h>

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

NSString * Const NAMEKEY = @ "name";

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

About iOS Const,static,extern

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.