我有一个CFArrayRef
主要有CFDictionaryRef
,但有时它会包含其他东西.如果可以的话,我想从数组中的字典中访问一个值,如果不能,我不会崩溃.这是代码:
bool result = false; CFArrayRef devices = CFArrayCreateCopy(kCFAllocatorDefault, SDMMobileDevice->deviceList); if (devices) { for (uint32_t i = 0; i < CFArrayGetCount(devices); i++) { CFDictionaryRef device = CFArrayGetValueAtIndex(devices, i); if (device) { // *** I need to verify this is actually a dictionary or actually responds to the getObjectForKey selector! *** CFNumberRef idNumber = CFDictionaryGetValue(device, CFSTR("DeviceID")); if (idNumber) { uint32_t fetched_id = 0; CFNumberGetValue(idNumber, 0x3, &fetched_id); if (fetched_id == device_id) { result = true; break; } } } } CFRelease(devices); } return result;
有关如何确保我只对CFDictionary这样的设备进行处理的任何建议,如果它是正确的吗?
(我正在处理一些没有特别好记录的开源代码,它似乎也不是特别可靠.我不确定这个数组是否包含非字典对象或bug的错误它没有检测到它何时包含非字典对象,但在我看来,在这里添加一个检查不太可能破坏其他代码然后强制它只包含其他地方的字典.我不经常使用CoreFoundation,所以我不确定我是否使用了正确的条款.)