sayguh
sayguh

Reputation: 2550

How can I do a multidimensional lookup table in Objective-C?

New to Objective-C here...

I have a unique string code as the key that I'd like to use to look up associated values.

It seems an NSDictionary is what I'm looking for but I believe this is only used to lookup one value for a given key.

Can anyone provide the code on how to declare/fill a multidimensional immutable NSDictionary? Also the code to retrieve the values?

Also, please let me know I'm going about this the wrong way

Thanks

Upvotes: 0

Views: 2006

Answers (3)

iandotkelly
iandotkelly

Reputation: 9124

EDIT: example of array within a dictionary .... using example data from your comment.

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
NSArray *firstSet = [[NSArray alloc] initWithObjects:43, 22];
NSArray *secondSet = [[NSArray alloc] initWithObjects:32, 50];

[myDictionary setValue:firstSet forKey:@"010"];
[myDictionary setValue:secondSet forKey:@"011"];

// Get a value out - should be the 50 that you want
NSInteger *myValue = [[myDictionary objectForKey:@"011"] objectAtIndex:1];

Not tested - but should be 95% right. Does this make sense?

Upvotes: 1

vikingosegundo
vikingosegundo

Reputation: 52227

For nested NSDictionaries or custom KVC-complient classes you can use keyPaths, and for nested NSArrays indexPaths.

Also stackoverflow will give you many examples.

Upvotes: 1

Andy
Andy

Reputation: 30418

You can make any object you want be the value for a given key in an NSDictionary. This includes NSArray or even another NSDictionary. Using either of these would allow you to associate multiple values with one key.

Upvotes: 1

Related Questions