Static keywords in objective-C syntax
If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute
, Original address: http://www.xuanyusong.com/archives/408
Friends who have studied Java or C should be familiar with the static keyword? Declare a static variable in a class. If you want to use it in other classes or modify it without the new object, you can directly use its class name to get the object of this static variable, you can modify the value of this variable in other classes.
Static variables declared in the objective-C syntax cannot be directly accessed through the class name 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. Momo uses an example to parse the use of the static keyword in detail.
The method for creating a class is not much said. I have written this article before.
Myclass. h
Warning: there is no error in compiling static statements outside the interface, but the compiler will report a warning. In this case, such statements are not recognized by the editor.
Error: static write
An error is reported directly in the interface. Obviously, this syntax is not accepted.
+ (Void) addcount; pay attention to the + number before this method. It means that the Peugeot method is a static method. After the sign + sign, you do not need to create this object. You can directly call this static method using the class name. The-sign used before the method, and the method after the sign-sign must pass through the object of this class or be available in the original.
# Import <Foundation/Foundation. h> // warning // static int scount; @ interface myclass: nsobject {// incorrect syntax // static int scount;} + (void) addcount; @ end
Myclass. m
Variables declared by the static keyword must be placed outside implementation or in methods,
If you do not assign a value to it, the default value is 0. It is initialized only once when the program is started.
+ (Void) addcount because the + id is identified, this method does not need to be called using this class object. You can call this method directly by using the class name.
# Import "myclass. H "static int scount = 100; @ implementation myclass + (void) addcount {scount ++; nslog (@" static integer variable value: % d ", scount );} @ end
Main. m
You can directly call the addcount method by using the myclass class name without alloc.
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code [myclass addcount]; int retval = uiapplicationmain (argc, argv, nil, nil); [pool release]; return retval ;}
Run this program. The initialization value is 100. When the method is called, the value of ++ is 101.
Myclass. m
Define the static integer variable in the method and assign it a value of 100.
# Import "myclass. H "@ implementation myclass + (void) addcount {static int scount = 100; scount ++; nslog (@" static integer variable value: % d ", scount );} @ end
Main. m
Call this method five times in a loop to see what the result is. The result is 101 ..
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code for (INT I = 0; I <5; I ++) {[myclass addcount];} int retval = uiapplicationmain (argc, argv, nil, nil); [pool release]; return retval ;}
Programmers must believe in themselves and everything they see in their eyes ~~
It can be seen that even if the static variable is written in the method, its initialization is also at the startup of the program, once the program is started, static cannot be created. So the program calls this method five times here. The scount value does not change because of the re-creation of static scount, but keeps the scount value in the memory.
Static keywords are very important in any language. They have advantages and disadvantages. It is a good choice to make good use of static keywords ~
A few days ago, I was unable to handle the network tragedy ~~ Today, it's so nice to go to the massage shop for acupuncture and cupping, and Momo will say hello to the majority of basin friends. Nothing is so important, you know how to go to health care and exercise ..