2012-04-23 22 views
2

我试图实现[NSMutableDictionary dictionaryWithObjects:frames forKeys:items],但使用CFDictionary,因此我可以控制键和值回调。这是我得到的:Casting'__unsafe_unretained id *'to'const void **'

__unsafe_unretained id keys[itemCount]; 
[items getObjects:keys range:NSMakeRange(0, itemCount)]; 
__unsafe_unretained id values[itemCount]; 
[frames getObjects:values range:NSMakeRange(0, itemCount)]; 
CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, itemCount, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 

其中给出以下错误:Cast of an indirect pointer to an Objective-C pointer to 'const void **' is disallowed with ARC

我尝试过桥接的各种排列,但我似乎无法让它编译没有投诉。

任何帮助非常感谢!

回答

4

我还没有找到它的语法。 (编辑:贾斯汀的回答有语法)试试这个:

void *keys[itemCount]; 
[items getObjects:(__unsafe_unretained id *)(void *)keys range:NSMakeRange(0, itemCount)]; 

然后当你调用CFDictionaryCreate你并不需要强制转换。

+0

没有想到在(void *)之前添加(__unsafe_unretained id *),但它对我有用。谢谢! – 2012-05-04 17:22:26

0

尝试(__bridge const void **)keys

+0

不兼容类型的铸造 '__unsafe_unretained ID *' 到 '常量无效**'用__bridge转换 – Max 2012-04-23 01:43:37

1

你可以简单地将它转换为void*在这一点上:

CFDictionaryRef d = CFDictionaryCreate(kCFAllocatorDefault, 
         (void*)keys, 
         (void*)values, 
         itemCount, 
         &kCFTypeDictionaryKeyCallBacks, 
         &kCFTypeDictionaryValueCallBacks); 

您也可以考虑这种方法:

const void* keys[itemCount]; 
CFArrayGetValues((__bridge CFArrayRef)items, CFRangeMake(0, itemCount), keys); 
const void* values[itemCount]; 
CFArrayGetValues((__bridge CFArrayRef)frames, CFRangeMake(0, itemCount), values); 

CFDictionaryRef d = CFDictionaryCreate(kCFAllocatorDefault, 
             keys, 
             values, 
             itemCount, 
             &kCFTypeDictionaryKeyCallBacks, 
             &kCFTypeDictionaryValueCallBacks); 
+0

您的演员在ARC下无法工作(从Xcode 4.3.2开始),他解释说他想使用CFDictionary,因此他可以更改回调。 – 2012-04-23 07:33:25

+0

@robmayoff嗯......它使用ARC与4.3.2在这里 – justin 2012-04-23 07:36:49

+0

你是对的。我从来没有真正尝试过*没有*直接在你的评论中加入__bridge。你的演员工作。 '__bridge'失败。 – 2012-04-23 07:41:29

相关问题