Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to convert an objective c code to objective cpp and use it in my application. Given the following code:

        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];

I get an error when changing the file name from file.m to file.mm and building: "no matching function for call to 'CFBooleanGetValue'"

I tried to #include <CoreFoundation/CFNumber.h> but I get the same error (I guess it's an ifdef issue).

Do you know any way I can cast without the missing function? Should I try to use the same function anyway?

That is not what I experienced. I put OP's code into a main.mm file. After adding the static cast for the CFBooleanGetValue argument, the code compiled and linked without problems. No manual linking necessary. – Martin R Aug 24, 2017 at 21:15 @MartinR, Ah, right! I think all apps got linked implicit with CoreFoundation anyway. And Foundation.h already includes CoreFoundation.h. – Cy-4AH Aug 25, 2017 at 10:01
 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];
                What about CFGetTypeID(dynamicValue) == CFBooleanGetTypeID(), your example handles only CFNumberGetTypeID and CFStringGetTypeID, while my problem occurs when it's CFBooleanGetTypeID
– benams
                Aug 23, 2017 at 8:51

In C++ a conversion from a void * (here: CFTypeRef) to a pointer of a different type (here: CFBooleanRef aka __CFBoolean *) requires an explicit cast. So this would solve the issue:

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

But actually I would forget about the CF types and bridging, and change the code to

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"];

This compiles in Objective-C and Objective-C++.

Your first example won't compile in objective c++, because of the CFBooleanGetValue - that's the root of my problem... About the second one - is it safe to assume that the king is either NSNumber or NSString ? it means that Boolean is kind of NSNumber? – benams Aug 23, 2017 at 8:50 @benams: Actually I tried my first example in an .mm file and it compiled without problems. – If the value is something else then none of the if-blocks will be executed, and the boolean variable remains NO. That is the same behaviour as in your code. – Martin R Aug 23, 2017 at 8:52

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.