无法使用'class'作为NSDictionary中的键 [英] Unable to use 'class' as a key in NSDictionary
问题描述
我想在 NSDictionary
中使用类作为键。我查看了此问题的答案,我所拥有的几乎是相同;我使用 setObject:forKey:
。然而,XCode抱怨说,不兼容的指针类型发送类'id< NSCopying>'
的参数类'。我的调用是:
I'm trying to use a class as a key in an NSDictionary
. I looked at the answer to this question and what I have is pretty much the same; I'm using setObject: forKey:
. However, XCode complains, saying Incompatible pointer types sending 'Class' to parameter of type 'id<NSCopying>'
. The call I have is:
[_bugTypeToSerializerDictionary setObject: bugToStringSerializer
forKey: [bugToStringSerializer serializedObjectType]];
bugToStringSerializer
是 BugToStringSerializer
,具体实现实现 serializedObjectType
。具体实现的示例如下:
bugToStringSerializer
is an instance of BugToStringSerializer
whose concrete implementations implement serializedObjectType
. An example of a concrete implementation looks like this:
- (Class) serializedObjectType {
return [InfectableBug class];
}
我在这里做什么?
推荐答案
(似乎类确实符合 NSCopying
不 copy
和 copyWithZone:
消息(这就是为什么你可以安全地忽略警告)。它们的类型是仍然不是 id< NSCopying>
。)这就是为什么编译器抱怨。
(It seems that classes do conform to NSCopying
, however their type is not id <NSCopying>
.) classes do not conform to protocols. Of course the essential is that classes respond to the copy
and copyWithZone:
messages (and that's why you can safely ignore the warning in this case). Their type is still not id <NSCopying>
.) That's why the compiler complains.
如果你真的不想要这种丑陋的警告,只需执行一个显式的类型转换:
If you really don't want that ugly warning, just perform an explicit type conversion:
[dictionary setObject:object forKey:(id <NSCopying>)someClass];
这篇关于无法使用'class'作为NSDictionary中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!