当前位置:网站首页>Confusing basic concepts member variables local variables global variables

Confusing basic concepts member variables local variables global variables

2022-07-05 08:48:00 InfoQ

Member variables

@interface Person:NSObject
{
 int age;

@end

  • Variables written in braces of class declarations , We call it a member variable ( attribute 、 Instance variables )
  • Member variables can only be accessed through objects
  • Member variables cannot leave the class , After leaving the class, it is not a member variable
  • Member variables cannot be initialized while being defined
  • Storage : Pile up ( The storage space of the heap corresponding to the current object ). Data stored in the heap , Will not be released by the system , Only programmers can release


local variable

-(void)info{
 int age = 0;
}

  • Variables written in functions or code blocks , We call it a local variable
  • Scope : Start with the line of definition , Until braces or
    return
  • Local variables can be defined first and then initialized , It can also be defined and initialized at the same time
  • Storage : Stack . Data stored in the stack , The system will automatically release


Global variables

@implementation Person
int age = 0;
-(void)info{

}
@end

  • Variables written outside functions and braces , It's called all variables
  • Scope : Start with the line of definition , All the way to the end of the file
  • Global variables can be defined first and then initialized , It can also be defined and initialized at the same time
  • Storage : Static zone , As soon as the program starts, it allocates storage space , Not released until the end of the program
  • There are two types of global variables :
  • static
    Modify global variable , Available only in this source file

 static int age =3;
 @implementation Person
 @end

 -  stay OC Declared in the syntax of `static` Static variables cannot be accessed directly through class names in other classes , Its scope can only be in the declared .m In file  . However, you can call the method of this class to indirectly modify the value of this static variable
 - `static` The variables declared by the keyword must be placed in `implementation` outside , Or in the method , If you don't assign a value to it , The default is 0, It initializes only once the program starts up ( It is not initialized after the class is instantiated )
 -  take static Static variables are written in methods , Its initialization is also when the program starts up , Once the program starts `static` Cannot be created in

  • extern
    Modify global variable , Can be referenced by other classes .h The header file states

 //.h In file
 extern int age;

 .m Implement the assignment in the file

 //.m In file
 #import "Person.h"
 int age = 10;
 @implementation Person
 
 @end 

  • extern
    Can be placed before variables or functions , To represent the definition of a variable or function in another file , Prompt the compiler when it encounters this variable or function , Look for its definition in other modules . in addition ,
    extern
    It can also be used to specify Links
  • Be careful : Naming must be unique , To distinguish from global variables in other source files , If a global variable with the same name as another source file appears , It will report an error
  • null
  • difference :
  • extern
    Decorated global variables have external links by default , Scope is the whole project , Global variables defined in a file , In another file , adopt external Declaration of global variables , You can use global variables .
  • static
    Decorated global static variables , The scope is the file where the variable is declared .
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050842475838.html