44

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?

Nate
  • 31,017
  • 13
  • 83
  • 207
johnluttig
  • 750
  • 1
  • 9
  • 24

3 Answers3

79

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);
Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
  • How would this work the other way? As in, make 'myNSDictionary' from 'cfDict'? – johnluttig Aug 10 '11 at 22:22
  • I tried: `currentListing = (__bridge NSDictionary *) currentListingRef;` and received the error of: **"Incompatible types casting 'CFDictionaryRef *' (aka 'const struct __CFDictionary **') to 'NSDictionary *' with a __bridge cast"** – johnluttig Aug 10 '11 at 22:27
  • 4
    updated the answer with how to go both ways. The problem with your code is you have CFDictionarRef*, CFDictionaryRef is already a pointer type. – Joshua Weinberg Aug 10 '11 at 22:32
9

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.

Laimonas
  • 5,781
  • 2
  • 19
  • 20
  • 2
    CFBridgingRelease is correct, assuming that "..." means you created/copied a CFDictionaryRef. While this is most likely the case, you don't always want to transfer – feliun Apr 24 '14 at 20:25
-1

Try this code,

NSDictionary *ssidList = (__bridge NSDictionary*)myDict;
NSString *SSID = [ssidList valueForKey:@"SSID"];
MSA
  • 134
  • 7