objective-c中的类变量内存生命周期 [英] Class variable memory lifecycle in objective-c

查看:91
本文介绍了objective-c中的类变量内存生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在.m文件中声明一个类变量(与实例变量相对),并通过setter&吸气剂例如[MyClass setValue],[MyClass Value]

If I declare a class variable in .m file (contrast to instance variable) and access it by a setter & getter e.g. [MyClass setValue], [MyClass Value]

这个变量内存何时被分配,什么时候被释放?

When will this variable memory be allocated and when will it be freed?

只是一个新的问题,我想从一个以前的问题:

Just a new questions I thought of from a previous question: How to store relatively static but configurable information

推荐答案

实现类变量作为具有文件范围的变量(全局变量),即在函数或方法外声明的变量,例如:

Assuming you’re realising a class variable as a variable with file scope (a global variable), i.e., a variable declared outside of a function or a method, like:

#import "MyClass.h"

SomeType someClassVariable;

@implementation MyClass
…
@end

那么变量被称为具有静态存储持续时间。这意味着,在程序启动之前,为该变量分配内存并初始化变量。

then the variable is said to have static storage duration. This means that, prior to program startup, memory for that variable is allocated and the variable is initialised. Its corresponding memory address is constant and its lifetime is the entire execution of the program.

如果该变量是一个Objective-C对象,例如

If that variable is an Objective-C object, e.g.

#import "MyClass.h"

NSString *someVariable;

@implementation MyClass
…
@end

它在程序启动之前用 nil 初始化。您需要为其分配一个对象,例如在 + [MyClass initialize] 或在 + [MyClass setValue:] 。当您为其分配对象时,您必须拥有它 - 通常使用 -retain -copy 。考虑到你已经拥有了分配给变量的对象的所有权,对象的生命周期将是程序的整个执行。

it is initialised with nil prior to program startup. You’ll want to assign it an object, e.g. in +[MyClass initialize] or in +[MyClass setValue:]. When you assign it an object, you must take ownership of it — typically by using either -retain or -copy. Considering you’ve taken ownership of the object that’s been assigned to the variable, the lifetime of that object will be the entire execution of the program.

请注意,如果你分配另一个对象,该变量应该释放上一个对象,很像标准实现setters的实例变量。

Note that if you assign another object to that variable you should release the previous object, much like the standard implementation of setters for instance variables.

另外一个注意事项:通常将类变量声明为 static

One further note: it is common to declare class variables as static:

#import "MyClass.h"

static NSString *someVariable;

@implementation MyClass
…
@end

通过这样做,您指定了它们具有内部链接,即它们仅对已被声明的翻译单元(实现,.m文件)可见。 否则,像第一个例子一样,该变量被称为具有外部链接,并且可以被其他翻译单元访问(其他实现, .m文件)。它是公共类变量的实现

By doing this, you’re specifying they have internal linkage, i.e., they’re visible only to the translation unit (the implementation, .m file) where it’s been declared. It’s a realisation of private class variables. Otherwise, like in the first example, the variable is said to have external linkage and can be accessed by other translation units (other implementation, .m files). It’s a realisation of public class variables.

这篇关于objective-c中的类变量内存生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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