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

标签 macos swift swift2 iokit

好的,所以在 Swift 2 中,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>?

作为带有 & 的输入参数。成功后,您可以打开可选的 (带有可选的绑定(bind)), 将非托管对象转换为托管对象 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)
    }
}

关于macos - Swift2:初始化 UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?> 参数以传递给 IORegistryEntryCreateCFProperties 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814292/

相关文章:

macos - 如何判断NSScrollView当前是否正在滚动

ios - 如何检测外围设备何时停止广告日期,以便我可以删除该外围设备形式发现的设备列表?

ios - objective c 父类(super class)中的 protected 变量在 swift 子类中不可访问

ios - 如何设置本地通知在首次设置通知后每秒触发一次?

ios - UISwitch 无法正常工作

ios - Swift 2 - 在调试控制台中打印 UIAlertView

macos - luac.out : incompatible precompiled chunk

c++ - 如何将 .gif 文件放入构建目录

c - MacOS 从 Mavericks Lion 兼容的可执行文件编译

swift - 如果用户喜欢图像 swift 2 如何获取的逻辑问题