ObjectiveC - Key-Value-Pair Collection with duplicate Keys

こ雲淡風輕ζ 提交于 2019-12-23 12:34:56

问题


Does ObjectiveC provide a collection for Key-Value-Pairs, that allow a key to occur multiple times?

I try to parse a xml file into some simple structure. Every things is already working with nested NSDictionary, but now xml elements can occur multiple times.

Edit: My Solution

I choose an NSArray with KeyValuePairs, it turned out that I need something that is order sensitive, NSDictionary was not possible. Sideeffect: NSFastEnumeration is easy to implement this way for my collection.


回答1:


No, Cocoa doesn’t have such a collection. If you don’t want to use a third-party library for that you can simulate that by using a NSDictionary with NSArray values. Or you could take a look at the CHDataStructures framework.




回答2:


It wouldn't be a very good key....

Best thing is to have an NSDictionary and for each key an NSArray holding all values against that key.




回答3:


Or an NSArray holding as many NSDictionaries (or NSObjects each with a key and a value property) as you like. You will then be able to filter the array with a predicate to find all the occurrences of a particular key and their associated values.



来源:https://stackoverflow.com/questions/6278477/objectivec-key-value-pair-collection-with-duplicate-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!