Incompatible pointer types sending 'const CFStringRef' (aka 'const struct __CFString *const') to parameter of type 'id'

StackOverflow https://stackoverflow.com/questions/18160868

Question

I am trying this in my cocoa app to get the information of directory/Files in system. This method return me a dictionary with some key attribute listed

-(NSDictionary *) metadataForFileAtPath:(NSString *) path {
    NSURL *url = [[[NSURL alloc] initFileURLWithPath:path] autorelease];

    MDItemRef itemRef = MDItemCreateWithURL(NULL, (CFURLRef)url);
    NSArray *attributeNames = (NSArray *)MDItemCopyAttributeNames(itemRef);
    NSDictionary *attributes = (NSDictionary *) MDItemCopyAttributes(itemRef, (CFArrayRef) attributeNames);
    CFRelease(itemRef);

    // probably it is leaking memory (attributeNames and attributes), better check with Instruments

    return attributes;
}

Another method...

NSDictionary *dict = [self metadataForFileAtPath];
NSString *date = [dict objectForKey:kMDItemFSCreationDate];

When I do this I got a warning message " Incompatible pointer types sending 'const CFStringRef' (aka 'const struct __CFString *const') to parameter of type 'id' " I am trying to type cast them into string but it still exist. I didn't get where I am wrong.

Was it helpful?

Solution 2

My Problem is solved by doing this

NSString *date = [dict objectForKey:@"kMDItemFSCreationDate"];

Anyway thanks for your help.

OTHER TIPS

I am answering this stale question because the currently accepted answer is wrong. That answer says to do this:

NSString *date = [dict objectForKey:@"kMDItemFSCreationDate"];

While that will indeed compile, it will only work as intended if the value of kMDItemFSCreationDate happens to be the string "kMDItemFSCreationDate".

The issue is that kMDItemFSCreationDate is a CFStringRef (aka 'const struct __CFString *const'), but the method is expecting an NSString*. The correct way to turn it into the expected type is this:

NSString *date = [dict objectForKey:(__bridge NSString*) kMDItemFSCreationDate];

See Apple's Documentation on Toll-Free Bridged Types

Please Add "CFNetwork" framework from linkbinarywithlibrery.And check Other frameworks If Needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top