gpt4 book ai didi

objective-c - 如何释放静态 Objective-C 变量

转载 作者:太空狗 更新时间:2023-10-30 03:18:06 26 4
gpt4 key购买 nike

StackOverflow question "using static keyword in objective-c when defining a cached variable"引用文献 code from Example 4 of Xcode's TableViewSuite它定义了一个 static NSDateFormatter 并调用了 alloc 但从不调用 release

static 变量不应该被释放吗?如果是,它们应该在代码中的什么位置发布?如果没有,为什么不呢?

最佳答案

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

这取决于。如果变量只被初始化一次,并且应该在应用程序的整个生命周期内保持不变,那么不,它不应该被释放(无论如何,当应用程序退出时,它的内存基本上会被释放)。但是,如果静态变量的值发生变化,那么是的,当静态变量设置为新对象时,应该释放先前的对象。

关于objective-c - 如何释放静态 Objective-C 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com