Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting a CFDictionaryRef to NSDictionary?

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?

like image 872
johnluttig Avatar asked Aug 10 '11 22:08

johnluttig


2 Answers

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); 
like image 141
Joshua Weinberg Avatar answered Sep 19 '22 15:09

Joshua Weinberg


In ARC, this should be done this way:

CFDictionaryRef currentListingRef = ...;
NSDictionary *currentListing = CFBridgingRelease(currentListingRef);

This releases the CF object and transfers ownership of the object to ARC otherwise you should release CF object manually.

like image 31
Laimonas Avatar answered Sep 20 '22 15:09

Laimonas