Cocoa内存管理规则

标签 cocoa cocoa-touch memory-management

Cocoa“内存管理编程指南”文档说:

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.

这是否也适用于 [NSNumber numberWithFloat:] 或 [CAAnimation 动画] 等便捷方法?我是否应该释放这些?

谢谢!

最佳答案

对于便捷方法来说,该陈述仍然正确,特别是最后一句

Any other time you receive an object, you must not release it.

这意味着您必须释放这些对象,除非您事先明确向它们发送保留消息。

关于Cocoa内存管理规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3186259/

相关文章:

objective-c - NSDictionary 数据键的最大大小

objective-c - 子类化 NSComboBox 以在 PopUp 上显示 NSMenu

ios - 将键盘附件 View 背景颜色与键盘颜色相匹配

c++ - 最大化内存使用

c++ - 为什么 std::runtime_error 的 c'tor 需要对 std::string 的常量引用?

java - 什么时候必须关闭数据库?

objective-c - NSTextField 在不可编辑时停止调整大小

objective-c - NSArrayController 更改不会传播到绑定(bind)的 NSUserDefaults

iphone - 使用 UITabBarController 时如何共享 ManagedObjectContext

iphone - UIScrollView 内的动态 UILabel 内容