Static variable in ObjectiveC

2019-09-18 15:19发布

Static variables are variables allocated statically at compile time. My doubt is for what purpose some variables are declared statically? I didn't have used any static variable in my code till now. From the apple code http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Introduction/Intro.html in securityWrapper.m, there is a line

static SecKeyWrapper * __sharedKeyWrapper = nil; 

what is the use of statically allocating __sharedKeyWrapper here?

I have seen the use of static key word in so many codes. So kindly explain the use and benefits when static keyword.

Thanks in advance.

3条回答
对你真心纯属浪费
2楼-- · 2019-09-18 15:58

That file global is a singleton, used for sharing an instance of a class.

The reason it's static in Objective-C is internal linkage. In practice, this means the variable can not be seen outside the file it is declared in. If another file declares a variable with the same name, they're two different variables.

Keep in mind that the way Objective-C instances work, the instance won't actually be allocated automatically. Rather, you have a pointer to the instance. The code will still have to do something like:

if ( !_sharedKeyWrapper ) {
    _sharedKeyWrapper = [[SecKeyWraper alloc] initBlahBlah];
}

See links for more.

查看更多
beautiful°
3楼-- · 2019-09-18 16:05

When you define a new class of objects, you can decide what instance variables they should have. Every instance of the class will have its own copy of all the variables you declare; each object controls its own data.

However, you can't prescribe variables for the class object; there are no "class variable" counterparts to instance variables. Only internal data structures, initialized from the class definition, are provided for the class. The class object also has no access to the instance variables of any instances; it can't initialize, read, or alter them.

Therefore, for all the instances of a class to share data, an external variable of some sort is required. Some classes declare static variables and provide class methods to manage them. (Declaring a variable static in the same file as the class definition limits its scope to just the class-and to just the part of the class that's implemented in the file. Unlike instance variables, static variables can't be inherited by subclasses.)

Static variables help give the class object more functionality than just that of a "factory" producing instances; it can approach being a complete and versatile object in its own right. A class object can be used to coordinate the instances it creates, dispense instances from lists of objects already created, or manage other processes essential to the application. In the case when you need only one object of a particular class, you can put all the object's state into static variables and use only class methods. This saves the step of allocating and initializing an instance.

查看更多
成全新的幸福
4楼-- · 2019-09-18 16:06

Also static variables are initialized once. You can use static variables in recursive calls. Simple Example Factorial.

查看更多
登录 后发表回答