0

我正在使用一个供应商 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 )
4

1 回答 1

0

具有讽刺意味的是,您的代码中唯一的错误是在核心基础中,非 ARC 的东西。您的+someCFCreateFunctionFromVendor方法调用CFNumberCreate()and ,它们都是 Create 函数,但在将对象添加到字典后CFHostCreateWithName()不会调用它们。CFRelease()它应该。否则,就是泄漏。顺便说一下,静态分析器会捕捉到这一点。(对于CFNumber,这意味着您必须将创建拆分为单独的行,以便您可以在将对象添加到字典后引用它。)

您不能释放宿主对象,+myMethod因为该代码没有获得它的所有权。它与ARC无关。您也不会在 MRR(手动保留/释放)代码中发布它。

于 2013-06-05T21:12:32.787 回答