所有类方法是否返回一个自动释放的对象? [英] Do all class methods return an autoreleased object?

查看:88
本文介绍了所有类方法是否返回一个自动释放的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新阅读Mac OS X的Cocoa编程的前几章,作者指出, NSCalendarDate 的类方法之一返回一个自动释放的对象。我总是假定所有的类方法返回一个自动释放对象(因为没有 alloc )。

I'm re-reading the first few chapters of Cocoa Programming for Mac OS X and the author states that one of NSCalendarDate's class method returns an autoreleased object. I always assumed that all class methods returned an autoreleased object (since there's no alloc involved).

感谢。

推荐答案

类方法,就像实例方法一样,应该遵守标准可可内存管理规则

Class methods, just like instance methods, should adhere to the standard Cocoa memory management rules.


如果
创建一个对象,一个方法的名字
以alloc或new开头,或者
包含copy(例如alloc,
newObject或mutableCopy),或者如果你
send它是一个保留消息。你是
负责放弃
拥有你拥有的对象使用
发布或autorelease。任何其他时间
你收到一个对象,你不能
释放它。

You take ownership of an object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

自动释放对象,或对单例的引用或类似的东西。无论如何,你不需要释放对象,除非它以alloc或new或包含copy开头。你不需要保留它,除非你想保持它超过当前自动释放池的范围,通过存储在一个iVar或类似的东西。

Presumably they are returning an autoreleased object, or a reference to a singleton or something like that. Either way, you need not release the object unless it started with "alloc" or "new" or contained "copy". You need not retain it unless you're looking to keep it around past the scope of the current autorelease pool, by storing it in an iVar or something like that.

这篇关于所有类方法是否返回一个自动释放的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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