Swift: CGPathRelease and ARC

安稳与你 提交于 2019-12-30 18:26:13

问题


Just updated to Xcode Beta 4, and noticed the following compiler error with my code below:

var path = CGPathCreateMutable()
...
CGPathRelease(path)

'CGPathRelease' is unavailable: Core Foundation objects are automatically memory managed

So do I simply just remove my release calls and everything should be fine? Or is there something more I'm missing? And are there any special cases I should be aware of with ARC?


回答1:


The Working with Cocoa Data Types section of Using Swift with Cocoa and Objective-C says (emphasis mine):

Core Foundation objects returned from annotated APIs are automatically memory managed in Swift—you do not need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself. If you return Core Foundation objects from your own C functions and Objective-C methods, annotate them with either CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED.

When Swift imports APIs that have not been annotated, the compiler cannot automatically memory manage the returned Core Foundation objects. Swift wraps these returned Core Foundation objects in an Unmanaged<T> structure.

So yes, unless you have an Unmanaged struct, this is correct and you don't have to worry about manually releasing the object.



来源:https://stackoverflow.com/questions/24900595/swift-cgpathrelease-and-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!