Casting a CFDictionaryRef to NSDictionary?

2019-01-23 22:48发布

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?

3条回答
劫难
2楼-- · 2019-01-23 23:10

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.

查看更多
3楼-- · 2019-01-23 23:11

Try this code,

NSDictionary *ssidList = (__bridge NSDictionary*)myDict;
NSString *SSID = [ssidList valueForKey:@"SSID"];
查看更多
干净又极端
4楼-- · 2019-01-23 23:21

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);
查看更多
登录 后发表回答