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

发布于 2024-07-26 00:46:35 字数 414 浏览 5 评论 0原文

我不太理解在接口实现中定义的静态变量。 在方法中,我确实理解它们与局部变量的不同之处,但不理解直接在实现中定义时的情况。

看看这些例子。 这两者实际上有什么区别?

#include "MyClass.h"

@implementation MyClass
int myInt;
...
@end

并且:

#include "MyClass.h"

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是我的挚爱i 2024-08-02 00:46:35

不幸的是,根据使用地点的不同,它会产生不同的效果。

静态函数:
默认情况下,所有函数都具有全局作用域。 static 说明符允许您将函数的范围限制为当前文件。

静态局部变量:
当您在局部变量上使用 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;
}

static 关键字的使用不会影响局部变量的范围。
详细了解静态关键字

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.

牵你的手,一向走下去 2024-08-02 00:46:35

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

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.

执妄 2024-08-02 00:46:35

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

请在此处查看完整说明:

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

"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

我要还你自由 2024-08-02 00:46:35

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文