使用 UIColor RGB black 作为 NSMutableDictionary 中的键时出现问题

发布于 2024-10-08 07:58:16 字数 572 浏览 0 评论 0原文

我可以整天将 UIColor 对象设置为 NSMutableDictionary 中的键,一切都很好,很开心...

例如:

[myDict setObject:foo forKey:[UIColor redColor]]; 

这很好用...除非我尝试使用以下内容:

UIColor *black = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1];
[myDict setObject:foo forKey:black];

这总是给我:

-[UIDeviceRGBColor copyWithZone: ]: 无法识别的选择器发送到实例 0x691be80

我在 RGB 色彩空间中定义黑色的原因对于这个问题并不重要,只需知道我必须这样定义它即可。我不明白的是为什么这个并且只有这个颜色给我带来了问题,为什么错误是 copyWithZone 错误?

根据记录,[UIColor blackColor] 作为键,但因为它不是 RGB 色彩空间,所以不适合我的应用程序。

I can set UIColor objects as keys in an NSMutableDictionary all day long and everything is fine and happy...

For instance:

[myDict setObject:foo forKey:[UIColor redColor]]; 

That works just fine... UNLESS I try to use the following:

UIColor *black = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1];
[myDict setObject:foo forKey:black];

That always gives me:

-[UIDeviceRGBColor copyWithZone:]: unrecognized selector sent to instance 0x691be80

The reasons for why I'm defining black in the RGB colorspace are unimportant to this question, just know that I must define it that way. What I don't understand is why is this and only this color causing me a problem, and why is it that the error is a copyWithZone error?

For the record, [UIColor blackColor] works as a key, but because it isn't an RGB colorspace it is not suitable for my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2024-10-15 07:58:16

看起来更好的问题是“为什么其他所有 UIColor 实例都充当键”?

NSMutableDictionary 中的键“必须符合 NSCopying 协议”。 UIColor 对象不符合该协议 根据文档,因此理论上您无法将它们用作字典中的键。

在实践中,我可能会猜测您会从 UIColor 隐藏的类簇中获取各种具体子类,也许其中一些子类实际上支持 NSCopying,但 UIDeviceRGBColor代码> 似乎没有。

您可以将 UIColor 对象包装在一个支持 NSCopying 的薄包装对象中,并覆盖 -isEqualTo: 等以使比较正常工作,然后在字典。

It looks like the better question is "why does every other UIColor instance work as a key"?

Keys in an NSMutableDictionary "must conform to the NSCopying protocol". UIColor objects don't conform to that protocol per the documentation, so you're theoretically unable to use them as keys in a dictionary.

In practice, I might guess that you're getting various concrete subclasses from a class cluster that's hidden by UIColor, and maybe some of them do, in fact, support NSCopying, but UIDeviceRGBColor does not seem to.

You could wrap the UIColor object in a thin wrapper object that did support NSCopying, and override -isEqualTo: etc to get the comparisons to work correctly, and then use those wrappers in the dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文