views:

1404

answers:

6

NSDictionary is good for key-value pairs, by what data structure is best for when you have three values? Is is best to create a class for those 3 values, and then let each object in an array contain instances of that class?

To be specific: The data structure, let's call it Person, I envisage has three values: (NSString)name, (int)age, (BOOL)isAlive

These would be held in array(?). It's important to be able to both use the name as a key, and also refer to the whole block with a number, starting at zero.

A: 

Isn't that the description of a two dimensional array?

MiRAGe
You're thinking of an array where each element links to an array with 3 elements. However, a two-dimensional array has nothing to do with storing key-value pairs — that requires a dictionary, something you might also see referred to as a "map", "associative array", "hash", etc. Specifically, NSDictionary supports pairs of 1 key to 1 value — he's looking for 1 key to 3 values.
Quinn Taylor
+7  A: 

If you have one key and two values, you would probably create a class for the two values and still use a dictionary.

If you have two keys and one value, you would probably use a nested dictionary - that is, a dictionary where the key is the first key and the value is another dictionary. The key for the nested dictionary would be the second key, and the value would be your actual value.

ETA:

Now that you've clarified your question - if you need to access your collection both by key or by integer index, you can still use a dictionary.

To get an item by key, you would do this:

[myDict objectForKey:@"joe"];

To get an item by index, you could use the allKeys array:

[myDict objectForKey:[[myDict allKeys] objectAtIndex:1]];

If the only purpose for getting the people by index is looping over the entire collection, you'd be better off looking into using the keyEnumerator method to enumerate the keys in a loop and use those keys to look up the values.

Eric Petroelje
A: 

It depends if you are trying to make some kind of generalized data structure or not. If the 'three' things are particular concepts within your app then you are may be heading towards a domain model - in which case you might be wise to encapsulate these values in a class.

If you require dictionary like functionality - [NSDictionary objectForKey:] etc. - then you can still use one or more dictionaries as indexes that map key values to your class instances.

teabot
A: 

You can use core data to represent more complex data relationships, using a relational database model.

Check out apple's developer site for examples and documentation.

Alex Brown
A: 

You need to look into CoreData(if you are storing it) and defining a class for your object. The native generic objects like NSDictionary aren't meant for storing complex values that make up complex objects like a Person.

Person *person = [[Person alloc] init];
person.firstName = @"blah";
person.age = 24;
person.isAlive = YES;
Jab
A: 

It may seem unrelated at first, but check out the dictionary-oriented structure in this Introduction to Cocoa Bindings. (CocoaDevCentral and Scott Stevenson are great resources for new Cocoa developers.) The approach he uses is extremely flexible and easy to change down the road. Using a dictionary instead of ivars isn't always the best solution, but it might be a good fit for your needs.

Note: Like Core Data, Cocoa Bindings is also a very complex topic. I'm suggesting using only the organizational aspect of the tutorial, not going all-out with bindings. :-)

Quinn Taylor