Global Variables:
Class, outside of the method, the class directly write NSString * str = @ "xxx"; STR is a global variable (object oriented, to use global variables, generally set as properties of the Class)
Local Variables:
Variables written in a method within a class are local variables.
Attention:
const, static, extern is just three variable modifier
const Determines whether a variable can be changed by static to
determine the lifetime of a variable the
cross-file value that can be used for no useful purpose.
..... As for the scope of the variables, they have nothing to do, only the variable definition of the place.
Const constant (read-only)
(When you don't want others to change the value of this variable, write it in a PCH to play the role of a macro)
Location can be written casually
Const effect
1. For modifying the right variable (basic variable, pointer variable)
2. Const-Modified variable read-only
The difference between a const and a macro
1. The compile time is different: macro: Precompiled const: Compile
2. Compile check: macros do not compile check const will
3. Macro Benefits: Macros can define functions and methods const cannot be
4. Macro disadvantage: Large use of macros can lead to long precompilation times
A constant pointer is distinguished from a pointer constant :
Look at the order of the pointer * and the const
const in front of the * number, indicating that there is a constant value, const int * a = &b; Can directly change the value of B, but can not be changed by *a, the compiler thinks that a is a constant
* number in front of the const, indicating that the created pointer variable can no longer be assigned, int * Const A = &b,a can no longer be assigned to another address
Related Topics
int * const P1; P1: Read -only *P1: variable
(const int)/(int const) *P2;//p2: variable *p2: Read
-only (const int)/(int const) * const P4;//P4: Read Only *P4: Read-only
the location of the const in iOS
Const modifies the string in iOS, where the const position is irrelevant, because the re-assignment will give an error
Causes, such as:
const nsstring * str = @ "ss"; / NSString * Const STR = @ "ss";
If you assign the value str = @ "string", you will get
an error because str = @ "string" essentially did two things:
1. Change the memory address of STR to address 2 @ "string"
. The value stored in STR memory is changed to string
so there is a problem with the re-assignment regardless of the const location.
Life cycle
There is a constant data area that is the same as the application life cycle
use of const in development
1. const replace macro, Macro: Commonly used string or basic data definition macro, const
2. Modifying method parameters, making method parameters read-only
static (Lift life cycle):
If you sometimes want a variable in the class to have a general long declaration period throughout the app's run, the controller destroys it and the variable still exists, using this modifier)
Scope (does not change the scope of the variable):
Modifying a local variable, as long as the local variable is modified by static, the declaration period of the local variable will be extended (the entire app is running), the scope is invariant to modify the global variable,
as long as the static modification of the global variable, the global variable can only be used under the current file
Allocation time :
Memory is allocated when the program is running
Life cycle :
A static modified variable is always present during the run of the program,
but it is initialized at compile time, and the code is no longer run when it is run.
Note the point:
The initialization of a global variable and a static variable in C99 must be a constant expression
static Nsarray * Array = @[]/[nsarray array] error: initializer element is Not a Compile-time constant
can only:
static Nsarray * array;
other places assignment
static NSString * str = @ "" This is OK , NSString is always special.
extern (cross-file use)
extern modified variable, name cannot be the same in global, otherwise error: variable redefined)
How to use (cross-file use):
A class has a property: int a = 8; outside the class, inside the class: Outside the method.
want to use in class B: extern int A; Position casually, outside the class, within the class: method, outside
the method at this time a value of a, B can be obtained, but also can be modified. Once modified, the value of a in a file also changed.
Scope
Global scope (only needed to be defined in one source file, which can be used for all source files)
Life cycle
Always exists during program run
static and const combined use
static const NSString * Reuseid = @ "Reuseid"; Read-only variables available within the current file
How do you avoid repeating definitions in development?
Practice: As long as you define global variables, can not be defined in their own class, in general development, we will have a public const file to define the global variables