Objective-C 中的静态变量 - 它们是做什么的? [英] static variables in Objective-C - what do they do?

查看:22
本文介绍了Objective-C 中的静态变量 - 它们是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些讨论静态变量是什么的帖子,我想我明白了 - 但我很想快速编写(或找到)一个程序,它同时使用常规变量和静态变量,并查看它们如何/何时以不同的方式运作.一些快速的脏代码,也许是两个 int vars 和几个 NSLog 跟踪语句,只是为了看看它们有何不同.

I've seen a few posts discussing what a static variable is and I think I get it - but I'd love to quickly write (or find) a program that utilizes both a regular and a static variable, side by side, and see how/when they operate differently. Some quick n dirty code, maybe two int vars and a couple of NSLog tracking statements just to see HOW they're different.

有人有任何提示/想法/代码可以说明静态变量与常规变量的不同之处吗?

Anybody got any tips/ideas/code out there that would illustrate how a static var differs from a regular one?

推荐答案

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

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.

假设你有这个:

int f(void)
{
    int i = 5;
    i += 10;
    return i;
}

每次调用 f() 都会返回值 15.

Every call to f() will return the value 15.

现在说你有这个:

int g(void)
{
    static int i = 5;
    i += 10;
    return i;
}

第一次调用 g() 时,将返回值 15.第二次返回 25,因为 i 保持 15 的值,然后自增 10.第三个调用 35 将被返回.等等.

The first time g() is called, the value 15 will be returned. The second time, 25 will be returned, as i maintained its value of 15 and then incremented itself by 10. The third call, 35 will be returned. And so on.

在Objective-C类的上下文中,静态变量经常被用来模拟类变量,因为Objective-C没有类变量(其他语言,比如Java,有).例如,假设您想延迟初始化一个对象,并且只返回该对象.您可能会看到:

In the context of Objective-C classes, static variables are often used to mimic class variables, as Objective-C does not have class variables (other languages, such as Java, do). For instance, say you want to lazily initialize an object, and only return that object. You might see this:

static MyObject *obj = nil;

@implementation MyObject

+ (id)sharedObject
{
    if (obj == nil) obj = [[MyObject alloc] init];
    return obj;
}

@end

obj 将在第一次调用 classObject 时被初始化;classObject 的后续调用将返回相同的对象.您可以通过记录对象的地址来检查这一点:

obj will be initialized the first time classObject is called; subsequent invocations of classObject will return the same object. You could check this by logging the address of the object:

NSLog(@"obj is at %p", [MyObject sharedObject]);
NSLog(@"obj is at %p", [MyObject sharedObject]);    // Will print the same address both times

此外,obj 将对 MyObject 中的所有方法可见.

Furthermore, obj will be visible to all methods in MyObject.

该技术也用于在 Objective-C 中实现单例类.

This technique is used to implemented singleton classes in Objective-C as well.

这篇关于Objective-C 中的静态变量 - 它们是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆