gpt4 book ai didi

objective-c - 如何知道一个对象是否自动释放?

转载 作者:太空狗 更新时间:2023-10-30 03:15:39 25 4
gpt4 key购买 nike

我对某些对象在我不知情的情况下被自动释放感到有点恼火。他们可能是件好事,但如果是的话,我想知道。文档没有说明自动释放对象的方法,所以我通常会测试我的前进方式,在我看来这是愚蠢的。例如,[NSDate date] 自动释放对象,[NSArray arrayWithObjects:...] 也是如此。没有文档告诉你,你怎么知道?

我开始看到一种模式,尽管像这样的方法(使用静态函数创建对象的方法)总是返回自动释放的对象。这总是正确的吗?

最佳答案

基本上,如果您initcopyretain 一个对象,您有责任释放它。如果不这样做,则您不负责发布它。

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

Many classes provide methods of the form +className... that you can use to obtain a new instance of the class. Often referred to as “convenience constructors”, these methods create a new instance of the class, initialize it, and return it for you to use. Although you might think you are responsible for releasing objects created in this manner, that is not the case according to the ownership policy set out earlier. Because the class creates the new object, it is responsible for disposing of the new object.

关于objective-c - 如何知道一个对象是否自动释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1466630/

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