objective-c 接口实现中静态变量的含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1087061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:52:21  来源:igfitidea点击:

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

objective-cvariablesstaticstatic-variables

提问by quano

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

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

myInt在这两种情况下对所有方法都是可见的,如果我解释了一个我正确运行的测试,myInt在这两种情况下对于类的不同实例都是相同的变量。

回答by appsunited

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.

静态局部变量:
当您在局部变量上使用 static 修饰符时,函数会在调用中“记住”其值。例如,以下代码段中的 currentCount 变量永远不会被重置,因此我们可以让 countByTwo() 为我们进行记录,而不是将计数存储在 main() 内部的变量中。

// 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.

static 关键字的这种使用不会影响局部变量的作用域。
阅读有关static 关键字的更多信息。

回答by smorgan

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.

该上下文中的 'static' 关键字与普通 C 中的关键字相同:它将 myInt 的范围限制为当前文件。

回答by smileBot

"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."

“在 C 和 Objective-C 中,静态变量是为程序的整个生命周期分配的变量。这与自动变量形成对比,自动变量的生命周期存在于单个函数调用中;以及动态分配的变量,如对象, 不再使用时可以从内存中释放。更简单地说,静态变量的值在所有函数/方法调用中都保持不变。在函数外部声明时,静态变量对其所在文件中的所有内容都是可见的声明;当在函数或方法内声明时,它仅在该函数或方法内可见,但值在调用之间保留。”

Check out the complete explanation here:

在这里查看完整的解释:

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

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

回答by c roald

From Apple's "The Objective-C Programming Language": "Declaring a variable staticlimits 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)."

来自 Apple 的“Objective-C 编程语言”:“声明一个静态变量将其作用域限制在类——以及文件中实现的类的一部分。(因此与实例变量不同,静态变量不能被继承由子类或直接由子类操作)。”