在目标c ++中将CFTypeRef转换为Boolean

时间:2017-08-22 15:23:06

标签: c++ objective-c casting objective-c++ core-foundation

我想将目标c代码转换为目标cpp并在我的应用程序中使用它。给出以下代码:

        CFTypeRef dynamicValue = (__bridge CFTypeRef)([[NSBundle bundleWithPath:aStr] objectForInfoDictionaryKey: @"CFPlugInDynamicRegistration"]);

        BOOL removeFlag = NO;

        if(dynamicValue == nil)
        {
            removeFlag = NO;
        }
        else if( CFGetTypeID(dynamicValue) == CFBooleanGetTypeID()  )
        {
            removeFlag = CFBooleanGetValue(dynamicValue);
        }
        else if( CFGetTypeID(dynamicValue) == CFStringGetTypeID()  )
        {
            removeFlag = ( [[(__bridge NSString*)dynamicValue lowercaseString] isEqualToString:@"yes"] ? YES:NO);
        }

        if( removeFlag )
        {       
            [unloadedArray addObject:aStr];
            [sortedArray removeObjectAtIndex:hoge];
        }

将文件名从file.m更改为file.mm并构建时出现错误:"没有匹配函数来调用' CFBooleanGetValue'"

我尝试#include <CoreFoundation/CFNumber.h>但我得到了同样的错误(我猜它是ifdef问题)。

你知道如果没有失踪功能我可以投出任何方式吗?我应该尝试使用相同的功能吗?

3 个答案:

答案 0 :(得分:1)

Objective-C ++没有自动框架链接。 您需要#include <CoreFoundation/CoreFoundation.h>并手动链接CoreFoundation框架。

答案 1 :(得分:0)

您可以使用此CFNumberGetTypeID()

 NSString *aStr = @"";

    CFTypeRef dynamicValue = (__bridge CFTypeRef)([[NSBundle bundleWithPath:aStr] objectForInfoDictionaryKey: @"CFPlugInDynamicRegistration"]);

    BOOL removeFlag = NO;

    if(dynamicValue == nil)
    {
        removeFlag = NO;
    }
    else if( CFGetTypeID(dynamicValue) == CFNumberGetTypeID()  )
    {
        removeFlag = [(__bridge NSNumber *)dynamicValue boolValue];
    }
    else if( CFGetTypeID(dynamicValue) == CFStringGetTypeID()  )
    {
        removeFlag = ( [[(__bridge NSString*)dynamicValue lowercaseString] isEqualToString:@"yes"] ? YES:NO);
    }

    if( removeFlag )
    {
        [unloadedArray addObject:aStr];
        [sortedArray removeObjectAtIndex:hoge];
    }

答案 2 :(得分:0)

在C ++中,从void *(此处:CFTypeRef)转换为 不同类型的指针(此处:CFBooleanRef又名__CFBoolean *)需要明确 投。所以这可以解决问题:

removeFlag = CFBooleanGetValue(static_cast<CFBooleanRef>(dynamicValue));

但实际上我会忘记CF类型和桥接,并将代码更改为

id dynamicValue = [[NSBundle bundleWithPath:@"path"] objectForInfoDictionaryKey: @"CFPlugInDynamicRegistration"];
BOOL removeFlag = NO;
if ([dynamicValue isKindOfClass:[NSNumber class]]) {
    removeFlag = ((NSNumber *)dynamicValue).boolValue;
} else if ([dynamicValue isKindOfClass:[NSString class]]) {
    removeFlag = [((NSString *)dynamicValue).lowercaseString isEqualToString:@"yes"];
}

这是在Objective-C和Objective-C ++中编译的。