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?
–
–
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];
–
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++.
–
–
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.