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

4.6k views Asked by At

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.

3

There are 3 answers

0
Surjeet Singh On BEST ANSWER

My Problem is solved by doing this

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

Anyway thanks for your help.

1
Friend On

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

0
Damon McCormick On

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