Objective-C HashMap等价 [英] Objective-C HashMap equivalent

查看:440
本文介绍了Objective-C HashMap等价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我试图转换一段Java代码,该代码使用一个包含对象和值作为对象的HashMap。 code> private static HashMap< Class< ;? extends Component>,ComponentType> componentTypes = new HashMap< Class< ;? extends Component>,ComponentType>();

我一直在阅读如何做到这一点Obj-C,但我没有成功,大多数人建议使用NSDictionary,问题是,他们的键需​​要是字符串,我需要他们作为对象。另一个选项是NSMapTable,但它不适用于iOS。



感谢,

解决方案

NSDictionary 的键不需要是字符串。它们可以是实现 NSCopying 的任何对象。但是,如果对象是自定义对象,则它需要对 -hash -isEqual:消息,但这与在Java集合中使用对象相同,因此它不应该是一个挑战。


I am trying to convert a piece of Java code which uses a HashMap that contains as a key an object and a value as an object.

private static HashMap<Class<? extends Component>, ComponentType> componentTypes = new HashMap<Class<? extends Component>, ComponentType>();

I've been reading on how to do this with Obj-C but I have not been successful, most people suggest using a NSDictionary, the problem is that they keys need to be strings and I need them as objects. The other option was NSMapTable, however it is not available on iOS. Would someone be able to assist on how I can convert this into an obj-c equivalent?

thanks,

解决方案

The keys for an NSDictionary do not need to be strings. They can be any object that implements NSCopying. If the object is a custom object, however, it needs to produce sane responses to the -hash and -isEqual: messages, but this is the same as using an object in a Java collection so it shouldn't be much of a challenge.

这篇关于Objective-C HashMap等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆