我正在使用一个供应商 API,它返回一个 CFDictionaryRef,它可以包含各种 CF 对象类型并且调用者需要 CFRelease。我想将其转换为 NSDictionary 以便更轻松地使用它,并希望确保我了解我在转换方面正确处理了元素。
在我看来,免费桥接类型(例如 CFString、CFNumber)只是由 NSDictionary 处理,如果它们一直是 Obj-C 类型,我就可以获取 NS 类型(我猜有一个桥梁演员在封面下进行)。
对于非免费桥接类型(例如 CFHost),看起来我可以将结果从 -valueForKey: 桥接到 CF 类型并从那里开始,但我不肯定是否需要释放该值.
这是一些说明问题的示例代码。这是处理事情的正确方法吗?
// Caller is responsible for releasing returned value
//
+ (CFDictionaryRef)someCFCreateFunctionFromVendor
{
CFMutableDictionaryRef cfdict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(cfdict, CFSTR("string1"), CFSTR("value"));
int i = 42;
CFDictionarySetValue(cfdict, CFSTR("int1"), CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &i));
CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, CFSTR("myhost"));
CFDictionarySetValue(cfdict, CFSTR("host1"), host);
return cfdict;
}
+ (void)myMethod
{
NSDictionary *dict = CFBridgingRelease([self someCFCreateFunctionFromVendor]);
for (NSString *key in [dict allKeys]) {
id value = [dict valueForKey:key];
NSLog(@"%@ class: %@", key, [value class]);
if ([value isKindOfClass:[NSString class]]) {
NSString *str = (NSString *)value;
NSLog(@"%@ is an NSString with value %@", key, str);
} else if ([value isKindOfClass:[NSNumber class]]) {
NSNumber *num = (NSNumber *)value;
NSLog(@"%@ is an NSNumber with value %@", key, num);
} else if ([value isKindOfClass:[NSHost class]]) {
NSLog(@"%@ is an NSHost", key); // never hit because no toll-free bridge to NSHost
} else {
NSLog(@"%@ is an unexpected class", key);
}
// Sample handling of non-toll-free bridged type
if ([key isEqualToString:@"host1"]) {
CFHostRef host = (__bridge CFHostRef)value;
NSArray *names = (__bridge NSArray *)(CFHostGetNames(host, false));
NSLog(@"host1 names: %@", names);
// I don't think I need to CFRelease(host) because ARC is still handling value
}
}
}
输出...
string1 class: __NSCFConstantString
string1 is an NSString with value strvalue
int1 class: __NSCFNumber
int1 is an NSNumber with value 42
host1 class: __NSCFType
host1 is an unexpected class
host1 names: ( myhost )