Objective-C自动发布 [英] objective-c autorelease

查看:80
本文介绍了Objective-C自动发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是obj-c的新手,无法理解函数自动释放.有人可以向我解释我什么时候应该使用它?以及与发布有何不同?还需要重置自动释放池吗?如何?什么时候?

Im new to obj-c and have trouble understanding the function autorelease. could someone explain to me when i should use it? and how is it different than release. also do I need to reset the autorelease pool? how? and when?

推荐答案

调用autorelease通过将对象添加到最顶部的NSAutoreleasePool来安排将release消息发送到不久的将来的某个对象.池收到drain消息时,会将release发送到已添加到其中的所有对象.

Calling autorelease schedules a release message to be sent to an object sometime in the near future by adding the object to the topmost NSAutoreleasePool. When a pool receives the drain message, it sends release to all the objects that have been added to it.

autorelease用于以下情况:方法或函数需要放弃其对对象的所有权,但需要防止其暂时被dealloc赋值,以便其调用者可以对其执行某些操作.在创建包装allocinitWith...autorelease的方便"方法以使分配对象的代码更简单时,它也很有用.

autorelease is used in situations where a method or function needs to relinquish its ownership of an object, but needs to keep it from being deallocated temporarily so that its caller can do something with it. It's also useful in creating "convenience" methods that wrap alloc, initWith... and autorelease to make code that allocates objects simpler.

这篇关于Objective-C自动发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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