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?


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) : @""; 

我使用以下来获得善意

    (NSURL *)path
    NSString *tempKind;
    [path getResourceValue:&tempKind forKey:NSURLLocalizedTypeDescriptionKey error:nil];
链接地址: http://www.djcxy.com/p/44918.html

上一篇: 指向'SEL'的C指针不允许使用ARC

下一篇: 似乎无法使用ARC从NSString *转换为CFString *