好的,所以在 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/