Objective-c variables and Objective-c Variables

Source: Internet
Author: User

Objective-c variables and Objective-c Variables

 

Language variables in OC can be divided into local variables and global variables by scope.

Local variables: these are also called internal variables. Local variables are declared within the method. Its scope is limited to the method. It is invalid to leave this method and then use this variable.

Global variable, also known as an external variable, is a variable defined externally by the method. It does not belong to any method, but belongs to a source program file. Its scope is the entire source program. Description of global variables: extern, for example, extern int intX;

If the external variable in a source file has the same name as the local variable, the external variable is "blocked" within the scope of the local variable, but does not work. Usually try not to use the extern variable.

Instance variables in the class can be used in each method. You can set access control for these instance variables.

The following example illustrates the differences between local variables and instance variables:

  

1 # import <Foundation/Foundation. h> 2 3 @ interface Test: NSObject {4 int _ x; // instance variable 5 int _ y; 6} 7 8-(void) setX :( int) x; 9-(int) x; 10 11-(void) setY :( int) y; 12-(int) y; 13 14-(void) print; 15 16 @ end17 18 @ implementation Test19 20-(void) setX :( int) x {21 _ x = x; 22} 23-(int) x {24 return _ x; 25} 26 27 28-(void) setY :( int) y {29 _ y = y; 30} 31-(int) y {32 return _ y; 33} 34 35-(void) print {36 int I = 1; // local variable 37 NSLog (@ "I = % I", I ); 38 NSLog (@ "add result: % I", _ x + _ y); 39} 40 41 42 @ end43 44 int main (int argc, const char * argv []) {45 @ autoreleasepool {46 Test * test = [[Test alloc] init]; 47 [test setX: 10]; 48 [test setY: 10]; 49 NSLog (@ "x = % I, y = % I", [test x], [test y]); 50 [test print]; 51} 52 return 0; 53}

 

Understand static variables:

Static variables are called static variables. Static variables have the following advantages: they can save memory because they are public to all objects. Therefore, for multiple objects, static variables are only stored in one place for all objects to share. The value of a static variable is the same for each object, but its value can be updated. As long as an object updates the value of a static variable, all objects can access the updated value, which improves efficiency. The following code demonstrates its usage:

  

 1 #import <Foundation/Foundation.h> 2  3 @interface Test : NSObject{ 4      5 } 6 - (int)staticIntY; 7 @end 8  9 @implementation Test10 11 static int Y = 10;12 13 - (int)staticIntY{14     Y+=1;15     return Y;16 }17 18 @end19 20 int main(int argc , const char *argv[]){21     @autoreleasepool {22         Test *test = [Test new];23         NSLog(@"Y = %i",[test staticIntY]);  // Y = 11;24         NSLog(@"Y = %i",[test staticIntY]);  // Y = 12;25         26     }27     return 0;28 }

 

Storage Class of variables:

We have used some variable Storage Class specifiers, such as extern and static. The following describes the other three variable Storage Class specifiers:

1. auto is used to declare an automatic global variable, which is the declaration method of the internal variables of the method. Generally, it is omitted, such as: auto int x = int x;

Note: Automatic variables do not have default values. Unless we assign values to them, their values are not definite.

2. const can declare the value in the program as an unchangeable value, which tells the compiler that this variable has a constant value during the program's operation. Is a constant, for example: const double pi = 3.14;

3. The volatile modifier is opposite to the const modifier, which tells the compiler that the value of this variable will change. For example:

* Char1 = 'a ';

* Char1 = 'B ';

If volatile is not used, when the compiler encounters the two lines of code, because this is to assign values to an address twice consecutively, therefore, the compiler deletes the first statement from the program. To prevent this situation. Char1 should be declared as a volatile variable;

Volatile char * char1;

 

The following example demonstrates the usage of the three identifiers:

  

1 # import <Foundation/Foundation. h> 2 3 @ interface Test: NSObject {4 5} 6 7-(void) print; 8 @ end 9 10 @ implementation Test11 12 const double d = 3.14; // indicates that d is not changed 13 14 volatile char c = 'C'; // indicates that c is variable at any time 15 16-(void) print {17 auto int I = 10; // local variable 18 NSLog (@ "I = % I", I); 19 NSLog (@ "d = % f ", d); 20 NSLog (@ "c = % c", c); 21} 22 23 @ end24 25 int main (int argc, const char * argv []) {26 @ autoreleasepool {27 Test * test = [Test new]; 28 [test print]; 29} 30 return 0; 31}

 

  

 

 

  

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.