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

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

问题描述

我看过几篇讨论什么是静态变量的文章,我想我明白了,但是我很想快速编写(或查找)一个同时使用常规变量和静态变量的程序,并查看它们如何/何时以不同的方式运行.一些快速的n脏代码,也许是两个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 变量都是为程序的整个生命周期分配的变量.这与 automatic 变量相反,后者的寿命存在于单个函数调用中.以及动态分配的变量(例如对象),这些变量在不再使用时可以从内存中释放.简而言之,在所有函数/方法调用中都会维护静态变量的值.当在函数外部声明静态变量时,对其声明的文件中的所有内容均可见;在函数或方法中声明时,仅在该函数或方法中可见,但在调用之间保留该值.

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.

现在说你有这个:

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天全站免登陆