Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sagarshivam

macrumors member
Original poster
May 24, 2011
51
0
Hi all!!

I am looking for some data structure which will store data in terms of key and value and where one key corresponds to one or more than one value.

So , something like:

KEY VALUE
key1 value1
key1 value2
key1 value3
key2 value4
key2 value5
...... ..........

and on query all VALUE for a particular KEY is obtained in array.

I tried implementing NSDictionary but I am able to save on one to one basis only Multiple values for one ley is not possible.

Kindly help

or say that I have three keys (string type)like: key1,key2 and key3 and corresponding to each I have three arrays (NSarray type) array1, array2 and array3

So i want to map these as key and values:


key1 ---------> array1
key2----------> array2
key3----------> array3

So that from this structure, whenever VALUE for KEY (say key1) is queried, array1 (and so on) is returned.
 
Last edited by a moderator:
multiset in STL?

Hi all!!

I am looking for some data structure which will store data in terms of key and value and where one key corresponds to one or more than one value.

...

So i want to map these as key and values:


key1 ---------> array1
key2----------> array2
key3----------> array3

So that from this structure, whenever VALUE for KEY (say key1) is queried, array1 (and so on) is returned.

Ok, so how do you use this thing? Is it set up statically at the beginning (of something) and then accessed by key to get that array? Is it dynamically created, where things get added (by key) to the arrays? The former is what jiminaus posted. The latter might be better done by defining your own class either as a subclass of NSMutableDictionary (or uses a NSMutableDictionary) to stores NSMutableArrays in the dictionary.

That's reasonably straightforward. Modify the setObject:forKey to append the object to current result of the objectForKey: (if there is one) or create a new NSMutableArray if there isn't one. Create new methods for other operations (if you want to delete an object from one of the arrays, you could define a removeObject:forKey: method).

I'm not sure why the documentation recommends against subclassing, but for a simple specialization like you describe it seems pretty safe.

<100 lines of code, 2 files, depending on how many operations you need.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.