简体   繁体   English

Class 变量 memory 生命周期在 objective-c

[英]Class variable memory lifecycle in objective-c

If I declare a class variable in.m file (contrast to instance variable) and access it by a setter & getter eg [MyClass setValue], [MyClass Value]如果我在.m 文件中声明一个 class 变量(与实例变量对比)并通过 setter 和 getter 访问它,例如 [MyClass setValue]、[MyClass Value]

When will this variable memory be allocated and when will it be freed?这个变量 memory 什么时候分配,什么时候释放?

Just a new questions I thought of from a previous question: How to store relatively static but configurable information只是我从上一个问题中想到的一个新问题: 如何存储相对static但可配置的信息

Assuming you're realising a class variable as a variable with file scope (a global variable ), ie, a variable declared outside of a function or a method, like:假设您将 class 变量实现为带有文件 scope全局变量)的变量,即在 function 或类似方法之外声明的变量,

#import "MyClass.h"

SomeType someClassVariable;

@implementation MyClass
…
@end

then the variable is said to have static storage duration.那么该变量据说具有static 存储持续时间。 This means that, prior to program startup, memory for that variable is allocated and the variable is initialised.这意味着,在程序启动之前,为该变量分配 memory 并初始化该变量。 Its corresponding memory address is constant and its lifetime is the entire execution of the program.它对应的memory地址是不变的,它的生命周期就是程序的整个执行过程。

If that variable is an Objective-C object, eg如果该变量是 Objective-C object,例如

#import "MyClass.h"

NSString *someVariable;

@implementation MyClass
…
@end

it is initialised with nil prior to program startup.它在程序启动之前用nil初始化。 You'll want to assign it an object, eg in +[MyClass initialize] or in +[MyClass setValue:] .您需要为其分配一个 object,例如在+[MyClass initialize]+[MyClass setValue:]中。 When you assign it an object, you must take ownership of it — typically by using either -retain or -copy .当您为其分配 object 时,您必须获得它的所有权——通常使用-retain-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.考虑到您已经拥有分配给变量的 object 的所有权,该 object 的生命周期将是程序的整个执行。

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.请注意,如果您将另一个 object 分配给该变量,您应该释放以前的 object,就像实例变量设置器的标准实现一样。

One further note: it is common to declare class variables as static :进一步说明:通常将 class 变量声明为static

#import "MyClass.h"

static NSString *someVariable;

@implementation MyClass
…
@end

By doing this, you're specifying they have internal linkage, ie, they're visible only to the translation unit (the implementation, .m file) where it's been declared.通过这样做,您指定它们具有内部链接,即它们仅对声明它的翻译单元(实现,.m 文件)可见。 It's a realisation of private class variables.它是私有 class 变量的实现。 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).否则,就像在第一个示例中一样,变量被称为具有外部链接并且可以被其他翻译单元(其他实现,.m 文件)访问。 It's a realisation of public class variables.它是公共 class 变量的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM