Objective-CMediumStatic variablesThe usage is described in this article,Objective-CGlobal supportVariableThere are two main implementation methods: the first one is the same as in C/C ++, and the "extern" keyword is used; the other is the singleton implementation. For exampleVariablePut it in AppDelegate as a globalVariableAppDelegate is a singleton class)
In Objective-C, how does one implement static member variables like in C ++?
What you need to do is in the implementation (. m or. mm. In this way, you do not need to create A class A instance to access static variables in other classes. Although this static variable is not A static member variable of Class A, it achieves the same effect. The scope of static variables is limited to a single file. The code can be as follows:
- //example.h
- @interface Example : NSObject {
-
- }
-
- - (id)init;
- +(int)instanceCount;
-
- @end
-
- //example.m
- #import "example.h"
-
- static int count;
-
- @implementation Example
- -(id)init{
- self = [super init];
- if(nil!=self){
- count+=1;
- }
- return self;
- }
-
- +(int)instanceCount{
- return count;
- }
-
- @end
- //example.h
- @interface Example : NSObject {
-
- }
-
- - (id)init;
- +(int)instanceCount;
-
- @end
-
-
- //example.m
- #import "example.h"
-
- static int count;
-
- @implementation Example
- -(id)init{
- self = [super init];
- if(nil!=self){
- count+=1;
- }
- return self;
- }
-
- +(int)instanceCount{
- return count;
- }
- @end
In the above Example, you can use [Example instanceCount]Static variablesYou do not need to create an instance.
Summary: DetailsObjective-CMediumStatic variablesI hope this article will help you with the introduction of the usage method.