Swift2:正确初始化UnsafeMutablePointer <Unmanaged <CFM​​utableDictionary>?>参数以传递给IORegistryEntryCreateCFProperties的方法

psm*_*irl 3 macos iokit swift swift2

好的,所以在Swift 2中,定义IORegistryEntryCreateCFProperties

func IORegistryEntryCreateCFProperties(entry: io_registry_entry_t, _ properties: UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?>, _ allocator: CFAllocator!, _ options: IOOptionBits) -> kern_return_t
Run Code Online (Sandbox Code Playgroud)

我可以

var dict: UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?> = nil
kr = IORegistryEntryCreateCFProperties(box as io_registry_entry_t, dict, kCFAllocatorDefault, nilOptions)
Run Code Online (Sandbox Code Playgroud)

编译和运行.当然,它在执行时崩溃,IORegistryEntryCreateCFProperties因为它dict被初始化为nil.我的问题是如何初始化dict为非零值?我尝试了各种方法但没有成功.

Mar*_*n R 5

类型的参数

UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?>
Run Code Online (Sandbox Code Playgroud)

意味着您必须传递该类型的变量

Unmanaged<CFMutableDictionary>?
Run Code Online (Sandbox Code Playgroud)

作为一个不合时宜的论点&.成功时,您可以解包可选(使用可选绑定),将非托管对象转换为托管对象 takeRetainedValue(),最后(如果需要),转换CFMutableDictionaryNSDictionary.

例:

var props : Unmanaged<CFMutableDictionary>?
if IORegistryEntryCreateCFProperties(entry, &props, kCFAllocatorDefault, 0) == KERN_SUCCESS {
    if let props = props {
        let dict = props.takeRetainedValue() as NSDictionary
        print(dict)
    }
}
Run Code Online (Sandbox Code Playgroud)