CFDictionaryGetValue抛出EXC_BAD_ACCESS

问题描述 投票:0回答:2

我在Objective-C的Getting graphic card information in objective C中发现了一段代码,目前正在尝试将其转换为Swift。我正在尝试从CFMutableDictionary中读取一个值(下面的代码)。但是,当我调用函数CFDictionaryGetValue时出现错误:“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x656d614e4f60)”

这是我当前的代码:

static func getGpuName() {
        var iterator: io_iterator_t = 0
        let errCode: kern_return_t  = IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOPCIDevice"), &iterator)
        if errCode != kIOReturnSuccess {
            fatalError("Could not retrieve the service dictionary of \"IOPCIDevice\"")
        }

        // iterate over the pci devices
        var device = IOIteratorNext(iterator)
        while device != 0 {
            var unmanagedServiceDictionary: Unmanaged<CFMutableDictionary>?
            if IORegistryEntryCreateCFProperties(device, &unmanagedServiceDictionary, kCFAllocatorDefault, 0) != kIOReturnSuccess {
                IOObjectRelease(device)
                continue
            }

            if let serviceDictionary: CFMutableDictionary = unmanagedServiceDictionary?.takeRetainedValue() {
                let name = CFDictionaryGetValue(serviceDictionary, "IOName")
            }

            // release the device
            IOObjectRelease(device)

            // get the next device from the iterator
            device = IOIteratorNext(iterator)
        }
}

有人知道如何读取CFMutableDictionary的值吗?

谢谢:)

objective-c swift macos core-foundation
2个回答
0
投票

好吧,经过更多研究后,我仍然不知道为什么会引发错误。但是,我通过将字典转换为NSDictionary找到了解决方法。

下面的代码现在可以使用:


0
投票

处理CoreFoundation API确实很痛苦。

© www.soinside.com 2019 - 2024. All rights reserved.