我有一个Core Foundation字典,我希望找到一个特定的值。当然,我会使用Core Foundation提供的CFDictionaryContainsValue()。第一个参数是要搜索的Core Foundation字典,第二个参数是var deviceToFind = "disk1"
var result: kern_return_t = KERN_FAILURE
let classesToMatch = IOServiceMatching(kIOMediaClass)
let classesToMatchDict = (classesToMatch as NSDictionary) as! Dictionary<String, AnyObject>
let matchedDevices = (classesToMatchDict as NSDictionary) as CFDictionaryRef
result = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatchCFDictRef, &storageIterator);
if CFDictionaryContainsValue(matchedDevices, &deviceToFind) == true
{
print("Found disk1")
}
类型。我在字典中寻找一个字符串,所以我尝试了这样的事情:
CFDictionaryContainsValue()
但是在调用var imagePath = ""
jQuery(".cheese").each(function (index) {
imagePat += jQuery(this).children("img").attr("src") + "\n";
})
时只会崩溃(EXC_BAD_ACCESS)。根据{{3}}我正确地构建了调用,但显然存在问题,但我不确定如何使其工作。
答案 0 :(得分:1)
要从IOReg获取BSD名称,您无法直接从matchedDevices
检索值。您必须遍历storageIterator
并分别获取BSD名称属性。
var deviceToFind = "disk1"
var storageIterator = io_iterator_t()
var object : io_object_t
var result: kern_return_t = KERN_FAILURE
let classesToMatchDict = IOServiceMatching("IOMedia")
result = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatchDict, &storageIterator)
if KERN_SUCCESS == result && storageIterator != 0 {
repeat {
object = IOIteratorNext(storageIterator)
if let data = IORegistryEntryCreateCFProperty(object, kIOBSDNameKey, kCFAllocatorDefault, 0), disk = data.takeRetainedValue() as? String {
if deviceToFind == disk {
print("Found \(deviceToFind)")
break
}
}
} while object != 0
IOObjectRelease(storageIterator)
}
答案 1 :(得分:0)
将CFDictionary强制转换为Swift字典,然后使用Swift原语可能会减少摩擦。
如果您需要留在CF-land,以下情况应该有效:
let value = "VALUE" as NSString
let dict = ["KEY": "VALUE"] as CFDictionary
if CFDictionaryContainsValue(dict, unsafeAddressOf(value)) {
print("contains value")
} else {
print("does not contain value")
}