OBJECTIVE-C syntax of the static keyword

Source: Internet
Author: User



Ext.: http://www.apkbus.com/android-593-1.html



A friend who has studied Java or C should be aware of the static keyword? A static static variable is declared in a class, or the other class wants to use it or modify it without the new object, directly using its class name to directly get the object of the statically variable, and can change the value of the variable arbitrarily in other classes.

Statically static variables declared in the OBJECTIVE-C syntax are not directly accessible through the class name in other classes, and can only be scoped to the declared. m file. However, you can invoke 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 of creating the class is not much to say, the previous article has been written.



MyClass.h



Warning: Static writing outside of interface is not an error, but the compiler will report a warning, so the wording is not approved by the editor.
Error: Static written in the interface will directly error, it is clear that such a grammar is not recognized.



+ (void) Addcount; Everyone, please note the above method in the front of the + number. It means Peugeot. This method is a static method, which can be called directly by the class name without creating the object after the sign + number. Before the method used before the-number, flag-number method must be through the object of this class or in the original can be used.


[Code]:
#import <Foundation / Foundation.h>

//caveat

// static int sCount;

@interface MyClass: NSObject

{

        // wrong writing

        // static int sCount;

}

+ (void) addCount;

@end





Myclass.m



The variable declared by the static keyword must be placed outside of implementation, or in a method, if it is not assigned the default value of 0, it is initialized only once on the program boot.



+ (void) Addcount This method does not need to use object invocation of this class because it identifies the + number. This method can be called directly using the class name.


[Code]java Code:
#import "MyClass.h"

static int sCount = 100;

@implementation MyClass

+ (void) addCount

{

     sCount ++;

     NSLog (@ "Static integer variable value:% d", sCount);

}

@end


Main.m



There is no need to alloc this object, directly using the MyClass class name to invoke the Addcount method directly.


[Code]:
#import <UIKit / UIKit.h>

#import "MyClass.h"

int main (int argc, char * argv [])

{

     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

     // Add our test code

     [MyClass addCount];

     int retVal = UIApplicationMain (argc, argv, nil, nil);

     [pool release];

     return retVal;

}





Run this program, initialize the assignment to 100, call the method at the time of + +, so print out the value of 101.






Myclass.m



The static integer variable is defined in the method and assigned a value of 100.


[Code]:
#import "MyClass.h"

@implementation MyClass

+ (void) addCount

{

     static int sCount = 100;

     sCount ++;

     NSLog (@ "Static integer variable value:% d", sCount);

}

@end





Main.m



Loop 5 times Call this method to see what the result is, the result is definitely 101.


[Code]:
#import <UIKit / UIKit.h>

#import "MyClass.h"

int main (int argc, char * argv [])

{

     NSAutoreleasePool * pool = [[NSAutoreleasePool 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 do not trust anyone to say anything about the program, you must believe in themselves, believe that their eyes see everything, wow ka ~ ~



Visible even if static variable is written in the method, its initialization is also when the program starts, the program once started after the static can not be created. So the program calls this method 5 times here, and the value of Scount is not changed by recreating the static Scount, but the value of Scount is always in memory.






The static keyword is very important in any language, it has advantages and disadvantages, the use of static keyword is a good choice oh, wow ka ka ~



OBJECTIVE-C syntax of the static keyword


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.