将CFDictionaryRef转换为NSDictionary?

时间:2011-08-10 22:10:02

标签: objective-c xcode ios4 casting nsdictionary

我有代码(剥离):

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

然后我收到错误:

  

强制转换非Objective-C指针类型'CFDictionaryRef *'(又名'const   ARC不允许使用struct __CFDictionary **')到'NSDictionary *'

我做错了什么?如何从CFDictionaryRef转换为NSDictionary

3 个答案:

答案 0 :(得分:66)

ARC改变了桥接的工作方式。

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

答案 1 :(得分:8)

在ARC中,应该这样做:

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

这将释放CF对象并将对象的所有权转移给ARC 否则你应该手动释放CF对象。

答案 2 :(得分:0)

试试这段代码,

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