In. NET, the keyword static used in static mode plays an important role. The static method can be called directly without instantiating class instances. The same is true for static attributes. The static keyword also exists in Object C. This keyword is used in today's learning process. Here we record the use of static. The static variables declared in the syntax of Object C cannot be directly accessed by class names in other classes. Its scope can only be in the declared. m file. However, you can call the method of this class to indirectly modify the value of this static variable. The usage and definition of classes in Object C have been recorded before. You can view the usage and definition of objects in Object C learning notes 3. 1. static attribute definition and. NET is a bit different. First, let's look at the following code: copy the Code # import <Foundation/Foundation. h> @ interface Person: NSObject {int age; NSString * name; static int totalCount;} @ property int age; @ property NSString * name;-(void) write; + (void) hello; @ end copy the code above to define static int totalCount; in this way, an error will be reported during compilation in the compiler. Such code compilation is not acceptable to the compiler. The correct definition is as follows: copy the Code # import <Foundation/Foundation. h> @ interface Person: NSObject {int age; NSString * name ;}@ property int age; @ property NSString * name;-(void) write; + (void) hello; @ end copy code copy Code # import "Person. h "static int count; @ implementation Person @ synthesize age; @ synthesize name;-(void) write {NSLog (@" name: % @ age: % I ", name, age) ;}+ (void) hello {count ++; NSLog (@ "static = % I", count) ;}@ end copy code static Attributes should be defined in implementation and written outside implementation or in methods. The above code writes the static attribute outside implementation. Copy the code @ autoreleasepool {for (int I = 0; I <5; I ++) {[Person hello] ;}} test result 22:03:14. 642 Object11 [488: 303] static = 12014-02-15 22:03:14. 644 Object11 [488: 303] static = 22014-02-15 22:03:14. 644 Object11 [488: 303] static = 32014-02-15 22:03:14. 645 Object11 [488: 303] static = 42014-02-15 22:03:14. 645 Object11 [488: 303] static = 5 copy the code. From the code above, we can see that the hello method is called to directly use the instance with the class name "Person" instead of "Person, in addition, after each call, count will increase by 1. 2. s The code for modifying the tatic attribute in the method is as follows: Change the static attribute to the method. Copy the Code # import "Person. h "@ implementation Person @ synthesize age; @ synthesize name;-(void) write {NSLog (@" name: % @ age: % I ", name, age );} + (void) hello {static int count; count ++; NSLog (@ "static = % I", count);} the code is copied in the same way as in method 1, the static attribute belongs to the global class. You can see how the test code works: copy the code @ autoreleasepool {for (int I = 0; I <5; I ++) {[Person hello] ;}/// test result 22:12:04. 681 Object11 [528: 303] static = 12014-02-15 22:12:04. 683 Obj Ect11 [528: 303] static = 22014-02-15 22:12:04. 684 Object11 [528: 303] static = 32014-02-15 22:12:04. 685 Object11 [528: 303] static = 42014-02-15 22:12:04. 685 Object11 [528: 303] static = 5 copy the code to the same effect as the previous one. Next, let's look at the static attributes defined in the instance method. The effect is as follows: modify the code as follows: copy the Code # import "Person. h "@ implementation Person @ synthesize age; @ synthesize name;-(void) write {static int myage = 0; myage ++; NSLog (@" age: % I ", myage) ;}+ (void) hello {static int count; count ++; NSLog (@ "static = % I", count );} the Static Property Test Method in the copy code test instance method is as follows: copy the code @ autoreleasepool {for (int I = 0; I <5; I ++) {Person * p = [[Person alloc] init]; p. name = [NSString stringWithFormat: @ "% @ % I ", @" object c ", I]; [p write] ;}// the test result is as follows: 22:20:35. 161 Object11 [582: 303] Name: object c 0 age: 12014-02-15 22:20:35. 163 Object11 [582: 303] Name: object c 1 Age: 22014-02-15 22:20:35. 164 Object11 [582: 303] Name: object c 2 age: 32014-02-15 22:20:35. 164 Object11 [582: 303] Name: object c 3 age: 42014-02-15 22:20:35. 164 Object11 [582: 303] Name: object c 4 age: 5 copy the code from the test code above. It can be seen that static attributes are also applicable to instance methods, in the process of calling the loop The name attribute of the new instance is changing, and the count value retains the result of the previous operation. This is the role of static. 3. static Methods defining static methods in. NET also need to apply static keywords, but not in Object C. Before we define a method, the format is as follows:-(Return Value Type) method name: parameter,... the applicable static method is to change "-" to "+. + (Void) hello; Define the above method signature in the interface, and implement this method in implementation. The static method has been mentioned in the above example. For details, refer!