objective-cmacoscocoaautomatic-ref-countingcfstring

Can't seem to cast from NSString* to CFString* using ARC


I am using the following code to obtain a file's file type in Objective-C (Mac OS X Cocoa Application):

NSString *kind = nil;
NSURL *url = [NSURL fileURLWithPath:[path stringByExpandingTildeInPath]];
LSCopyKindStringForURL((CFURLRef)url, (CFStringRef*)&kind);
return kind ? kind : @""; 

However, the only error being produced is on the following line:

LSCopyKindStringForURL((CFURLRef)url, (CFStringRef*)&kind);

Saying:

C-style cast from 'NSString *__strong *' to 'CFStringRef *' (aka 'const __CFString **') casts away qualifiers

After doing some research, I saw that the __bridge keyword is required to make the cast from NSString* to CFStringRef* valid in ARC. So I stuck in the keyword, producing:

LSCopyKindStringForURL((CFURLRef)url, (__bridge CFStringRef*)&kind);

Now, I am receiving the following error:

Incompatible types casting 'NSString *__strong *' to 'CFStringRef *' (aka 'const __CFString **') with a __bridge cast

It's starting to drive me crazy... no matter what I seem to try, I just can't make the error go away. Any ideas?


Solution

  • There's a simple workaround. Use an actual CFStringRef variable and cast it when it comes time to return it as an NSString:

    NSURL *url = [NSURL fileURLWithPath:[path stringByExpandingTildeInPath]];
    CFStringRef kind;
    LSCopyKindStringForURL((CFURLRef)url, &kind);
    return kind ? CFBridgingRelease(kind) : @"";