4

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.

Fox32
  • 13,126
  • 9
  • 50
  • 71
  • I used the accepted answer first, but later (some days) it turned out that it wasn't possible. If I wouldn't have these conditions, I would use the solution of Sven. – Fox32 Jun 16 '11 at 15:49

3 Answers3

4

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.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • 1
    for those of you setting multiple GET parms with AFHTTPClient, it will handle just that if you set the value with an NSArray – Max MacLeod Jun 21 '12 at 13:06
  • 1
    Thanks @MaxMacLeod. I found the AFN behavior which appends "[]" to the repeated parameter name undesirable, but a quick edit fixes that: https://gist.github.com/4113874 – phatblat Nov 19 '12 at 21:05
3

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.

Simon Lee
  • 22,304
  • 4
  • 41
  • 45
1

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.

Elise van Looij
  • 4,162
  • 3
  • 29
  • 52