如何释放静态Objective-C变量 [英] How to release static Objective-C variables

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

问题描述

StackOverflow问题使用静态关键字在Objective-c中定义缓存变量时" 引用

The StackOverflow question "using static keyword in objective-c when defining a cached variable" references code from Example 4 of Xcode's TableViewSuite that defines a static NSDateFormatter and calls alloc but never calls release.

不应该发布static变量吗?如果是,应在代码中的何处发布它们?如果没有,为什么不呢?

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

推荐答案

不应该发布static变量吗?如果是,应在代码中的何处发布它们?如果没有,为什么不呢?

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

这取决于.如果变量仅初始化一次,并且应在应用程序的整个生命周期内保持不变,则否,则不应释放该变量(无论如何,退出应用程序后,其内存实际上将被释放).但是,如果静态变量的值发生变化,则可以,当将静态变量设置为新对象时,应释放先前的对象.

It depends. If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its memory will essentially be freed when the application exits, anyway). If, however, the value of the static variable changes, then yes, the previous object should be released when the static variable is set to a new object.

这篇关于如何释放静态Objective-C变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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