关于返回的 Quartz 对象的所有权
我最近询问了有关自动释放返回的石英对象的问题: CGMutablePathRef 自动释放?
Dave DeLong 回答了我的问题,quartz(或任何 NS 基础对象)没有自动释放),我应该使用 创建规则。然而,该文件的命名约定表示,
Core Foundation 命名约定,特别是“create”一词的使用,仅适用于返回 Core Foundation 对象的 C 函数。 Objective-C 方法的命名约定受 Cocoa 约定管辖,无论该方法返回 Core Foundation 还是 Cocoa 对象。
由此看来,由于我的函数是目标 C 对象中的一条消息,因此将其命名为 createSomething 似乎并不合适。我仍然想返回这个对象。解决这个问题的最佳方法是什么?我应该使用 获取规则,然后让调用者显式保留它?但这并不在可可公约的范围内。处理这个问题的正确方法是什么?
I have recently asked about autoreleasing a returned quartz object:
Autorelease for CGMutablePathRef?
Dave DeLong answered my question that there is no autorelease for quartz (or any NS foundation objects) and I should use the Create Rule. However the naming convention on the document says,
The Core Foundation naming conventions, in particular use of the word “create”, only apply to C functions that return Core Foundation objects. Naming conventions for Objective-C methods are governed by the Cocoa conventions, irrespective of whether the method returns a Core Foundation or Cocoa object.
By this account since my function is a message in an objective C object it doesn't seem proper to name it createSomething. I still want to return this object. What's the best way to approach this? Should I use the Get Rule and then have the caller explicitly retain it? But this is not within Cocoa convention. What's the proper way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您应该从返回新对象的 Objective-C 方法返回一个自动释放的对象。使用 Core Foundation 对象可以很容易地做到这一点。例如,采用这个基本类方法:
请注意,上面的代码将在垃圾收集和引用计数环境中工作。如果您使用的是 iPhone,您可能需要执行以下操作:
Normally you should return an autoreleased object from an Objective-C method that returns a new object. It's easy enough to do this with Core Foundation objects. For example, take this basic class method:
Note that the above code will work in both a garbage-collected and reference-counted environment. If you're on the iPhone, you might need to do: