在Swift中使用对CFPropertyListRef的引用

时间:2021-06-04 02:02:27

I'm currently converting some Objective-C code to swift and I am stuck. I do have an internal API which fills a CFPropertyList and give back it's format.

我目前正在将一些Objective-C代码转换为swift而且我被卡住了。我有一个内部API,它填充CFPropertyList并返回它的格式。

MyFunction(CFPropertyListRef list, CFPropertyListFormat *fmt);

In Objective-C I'm calling it via

在Objective-C中,我通过它来调用它

CFDictionaryRef myList;
CFPropertyListFormat fmt;
MyFunction(&myList, &fmt)

Via the "Generated Interface" i can see that swift converted to

通过“Generated Interface”我可以看到swift转换为

MyFunction(_ list: CFPropertyList!, _ fmt: UnsafeMutablePointer<CFPropertyListFormat>))

When I'm trying to call the function in swift via

当我试图在swift中调用函数时

var fmt = CFPropertyListFormat.XMLFormat_v1_0
var plist = NSDictionary() as CFPropertyListRef
MyFunction(plist, &fmt)

I'm getting a EXC_BAD_ACCESS. As the compiler doesn't complain about the types I think this should be right. Any help is very appreciated! Thanks

我正在获得EXC_BAD_ACCESS。由于编译器没有抱怨我认为应该是正确的类型。任何帮助非常感谢!谢谢

1 个解决方案

#1


1  

If your usage in Objective-C is really right, the function header of your MyFunction needs to be something like this:

如果你在Objective-C中的用法非常正确,你的MyFunction的函数头必须是这样的:

extern void MyFunction(CFPropertyListRef *list, CFPropertyListFormat *fmt);

(CFPropertyListRef actually is just a typealias of void * in Objective-C/C. So, the compiler rarely outputs warnings for many possible type mis-uses.)

(CFPropertyListRef实际上只是Objective-C / C中void *的一个类型。因此,编译器很少输出许多可能的类型误用的警告。)

And Swift 2 imports such function as:

而Swift 2导入的功能如下:

public func MyFunction(list: UnsafeMutablePointer<Unmanaged<CFPropertyList>?>, _ fmt: UnsafeMutablePointer<CFPropertyListFormat>)

So, assuming you have changed the function header as above, you need to use it as follows:

因此,假设您已经更改了上面的函数头,则需要按如下方式使用它:

var fmt: CFPropertyListFormat = .XMLFormat_v1_0
var umPlist: Unmanaged<CFPropertyList>? = nil
MyFunction(&umPlist, &fmt)
var plist = umPlist?.takeRetainedValue() //or this may be `umPlist?.takeUnretainedValue()`

#1


1  

If your usage in Objective-C is really right, the function header of your MyFunction needs to be something like this:

如果你在Objective-C中的用法非常正确,你的MyFunction的函数头必须是这样的:

extern void MyFunction(CFPropertyListRef *list, CFPropertyListFormat *fmt);

(CFPropertyListRef actually is just a typealias of void * in Objective-C/C. So, the compiler rarely outputs warnings for many possible type mis-uses.)

(CFPropertyListRef实际上只是Objective-C / C中void *的一个类型。因此,编译器很少输出许多可能的类型误用的警告。)

And Swift 2 imports such function as:

而Swift 2导入的功能如下:

public func MyFunction(list: UnsafeMutablePointer<Unmanaged<CFPropertyList>?>, _ fmt: UnsafeMutablePointer<CFPropertyListFormat>)

So, assuming you have changed the function header as above, you need to use it as follows:

因此,假设您已经更改了上面的函数头,则需要按如下方式使用它:

var fmt: CFPropertyListFormat = .XMLFormat_v1_0
var umPlist: Unmanaged<CFPropertyList>? = nil
MyFunction(&umPlist, &fmt)
var plist = umPlist?.takeRetainedValue() //or this may be `umPlist?.takeUnretainedValue()`

相关文章