开发者

what is the best way to store this in a NSDictionary?

Say I have a dictionary of dictionary that is like this:

{0, {"Kansas City", TRUE}}
{1, {"Arizona", TRUE}}
{2, {"Mexico City", TRUE}}
{3, {"Arkansas", TRUE}}
{4, {"California", TRUE}}
{5, {"Illinois", TRUE}}

What is a better way to store this? The only problem I have with this is when I am trying to get the value of TRUE, I need to know the key.. if I don't know the key then I'd have to iterate over a dictionary.. however in this case I will always be iterating always just for one key - object pair.

Looking for some re开发者_运维知识库commendations


Are you sure to use a dictionary rather than an array? It seems that you need a sorted collection (1, 2, 3...). So, I would store NSDictionary objects (with a key represented by the city and a boolean value) into an NSArray... or otherwise, if index doesn't matter I would use a single NSDictionary.

ps. to retrieve boolean values you have to use boolValue


NSDictionary *cityDic = [NSDictionary dictionaryWithObjectsAndKeys:@"Kansas City", @"kCity", [NSNumber numberWithBool:TRUE], @"kBoolean", nil];

NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:0], cityDic, nil];

NSArray *totalArr= [NSArray arrayWithArray:arr];

NSLog(@"%@", totalArr);

2011-10-07 16:21:16.769 StackoverflowTest[552:f803] ( 0, { kBoolean = 1; kCity = "Kansas City"; } )

you access [[totalArr objectAtIndex:1] forKey:@"kCity or kBoolean"];


Seems like this is more an Array with dictionaries in them. If your first values are numbers (in this example at least) and you know the key (which is also a number then obviously) you can just [myArray objectAtIndex:i] and then do whatever you want with the dictionary within.

//---editted---//

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜