【问题标题】:Can I cast CFMutableDictionaryRef to CFDictionaryRef?我可以将 CFMutableDictionaryRef 转换为 CFDictionaryRef 吗?
【发布时间】:2020-06-30 06:42:23
【问题描述】:

函数期待 CFDictionaryRef 返回:

CFDictionaryRef xyz()
{
CFMutableDictionaryRef test = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(test, CFSTR("Test"), CFSTR("Test"));

return reinterpret_cast<CFDictionaryRef>(test);
}

可以使用 reinterpret_cast 进行转换吗?给我一个空指针。

【问题讨论】:

  • 你不能直接返回test吗?可变字典是字典的子类。

标签: c++ ios objective-c core-foundation reinterpret-cast


【解决方案1】:

您可以安全地执行以下操作:

CFDictionaryRef xyz()
{
    CFMutableDictionaryRef test = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(test, CFSTR("Test"), CFSTR("Test"));

    return (CFDictionaryRef)test;
}

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多