问题描述
我用我的雨燕项目中的Objective-C类通过一个桥接报头。该方法的签名看起来是这样的:
I'm using an Objective-C class in my Swift project via a bridging header. The method signature looks something like this:
- (CFArrayRef)someMethod:(someType)someParameter;
我开始通过获取类的一个实例,调用方法,和存储的值:
I started by getting an instance of the class, calling the method, and storing the value:
var myInstance = MyClassWithThatMethod();
var cfArr = myInstance.someMethod(someValue);
然后试图让数组中的值:
Then try to get a value in the array:
var valueInArrayThatIWant = CFArrayGetValueAtIndex(cfArr, 0);
不过,我得到的错误未管理< CFArray>'是不相同的'CFArray'
。这是什么未管理< CFArray>
甚至意味着
我通过但我并不需要把数组转换为迅速阵列(不过这将是很好)。我只需要能够从阵列中得到的值。
I looked through How to convert CFArray to Swift Array? but I don't need to convert the array to a swift array (however that would be nice). I just need to be able to get values from the array.
我也试图通过了 CFArray
成一个功能概述:
I have also tried the method of passing the CFArray
into a function outlined in this answer:
func doSomeStuffOnArray(myArray: NSArray) {
}
不过使用时,我收到了类似的错误:
However I get a similar error when using it:
doSomeStuffOnArray(cfArr); // Unmanaged<CFArray>' is not identical to 'NSArray'
我使用 CFArray
,因为我需要存储 CGPathRef
的数组,不能被保存在的NSArray
。
I am using CFArray
because I need to store an array of CGPathRef
, which cannot be stored in NSArray
.
所以,我怎么在斯威夫特使用 CFArray
?
So how am I supposed to use CFArray
in Swift?
推荐答案
正如上文
<一href=\"https://developer.apple.com/library/$p$prelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html\">Working与可可数据类型中,有两种可能的解决方案时,
As explained inWorking with Cocoa Data Types, there are two possible solutions whenyou return a Core Foundation object from your own function that is imported in Swift:
-
注释与
CF_RETURNS_RETAINED
函数或CF_RETURNS_NOT_RETAINED
。
你的情况:
Annotate the function with
CF_RETURNS_RETAINED
orCF_RETURNS_NOT_RETAINED
.In your case:
- (CFArrayRef)someMethod:(someType)someParameter CF_RETURNS_NOT_RETAINED;
或转换非托管对象到存储管理对象与 takeUnretainedValue()
或 takeRetainedValue()
斯威夫特。你的情况:
Or convert the unmanaged object to a memory managed object with takeUnretainedValue()
or takeRetainedValue()
in Swift. In your case:
var cfArr = myInstance.someMethod(someValue).takeUnretainedValue()
这篇关于我如何使用CFArrayRef斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!