What's the meaning of static variables in an implementation of an interface?

纵然是瞬间 提交于 2019-12-28 04:45:29

问题


I don't quite understand static variables when defined in the implementation of an interface. In methods I do understand how they differ from local variables, but not when defined directly in an implementation.

Look at these examples. What difference do these two make practically?

#include "MyClass.h"

@implementation MyClass
int myInt;
...
@end

And:

#include "MyClass.h"

@implementation MyClass
static int myInt;
...
@end

myInt is in both cases visible to all the methods, and if I interpreted a test I ran correctly, myInt will in both cases be the same variable for different instances of the class.


回答1:


The 'static' keyword in that context is the same as it would be in plain C: it limits the scope of myInt to the current file.




回答2:


Unfortunately, it has different effects depending on where you use it.

Static Functions:
By default, all functions have a global scope. The static specifier lets you limit the function’s scope to the current file.

Static Local Variables:
When you use the static modifier on a local variable, the function “remembers” its value across invocations. For example, the currentCount variable in the following snippet never gets reset, so instead of storing the count in a variable inside of main(), we can let countByTwo() do the recording for us.

// main.m
#import <Foundation/Foundation.h>

int countByTwo() {
    static int currentCount = 0;
    currentCount += 2;
    return currentCount;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"%d", countByTwo());    // 2
        NSLog(@"%d", countByTwo());    // 4
        NSLog(@"%d", countByTwo());    // 6
    }
    return 0;
}

This use of the static keyword does not affect the scope of local variables.
Read more about the static keyword.




回答3:


"In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program. This is in contrast to automatic variables, whose lifetime exists during a single function call; and dynamically-allocated variables like objects, which can be released from memory when no longer used. More simply put, a static variable's value is maintained throughout all function/method calls. When declared outside of a function, a static variable is visible to everything within the file in which it is declared; when declared inside a function or method, it is visible only within that function or method, but the value is retained between calls."

Check out the complete explanation here:

https://stackoverflow.com/a/4965145/951349




回答4:


From Apple's "The Objective-C Programming Language": "Declaring a variable static limits its scope to just the class -- and to just the part of the class that's implemented in the file. (Thus unlike instance variables, static variables cannot be inherited by, or directly manipulated by, subclasses)."



来源:https://stackoverflow.com/questions/1087061/whats-the-meaning-of-static-variables-in-an-implementation-of-an-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!