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

Swift2: Correct way to initialise UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?> parameter to pass to IORegistryEntryCreateCFProperties

好的,所以在Swift2中,IORegistryEntryCreateCFProperties的定义是

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

我能做到

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

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

类型的参数

UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?>

意味着你必须传递一个

类型的变量
Unmanaged<CFMutableDictionary>?

作为 & 的输入参数。成功后,您可以打开可选的 (带有可选的绑定), 将非托管对象转换为托管对象 takeRetainedValue(),最后(如果需要),将 CFMutableDictionary 转换为 NSDictionary

示例:

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