Casting a CFDictionaryRef to NSDictionary?

johnluttig picture johnluttig · Aug 11, 2011 · Viewed 24.8k times · Source

I have the code (stripped down):

CFDictionaryRef *currentListingRef;
//declare currentListingRef here
NSDictionary *currentListing;
currentListing = (NSDictionary *) currentListingRef;

And then I get the error:

Cast of a non-Objective-C pointer type 'CFDictionaryRef *' (aka 'const struct __CFDictionary **') to 'NSDictionary *' is disallowed with ARC

What am I doing wrong? How do I convert from a CFDictionaryRef to an NSDictionary?

Answer

Joshua Weinberg picture Joshua Weinberg · Aug 11, 2011

ARC changed the way bridging works.

NSDictionary *original = [NSDictionary dictionaryWithObject:@"World" forKey:@"Hello"];
CFDictionaryRef dict = (__bridge CFDictionaryRef)original;
NSDictionary *andBack = (__bridge NSDictionary*)dict;
NSLog(@"%@", andBack);