NSDictionary read data

不问归期 提交于 2019-12-04 14:12:07
e.James

If you are trying to save the game state for a number of players, you may be going about it the wrong way. I don't want to stand in the way of your current progress, but I would like to point out some avenues for improvement.

First point: NSDictionary objects (and plists, for that matter) can store data in a tree structure, so there is no need to have a different set of keys for each user:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSArray arrayWithObjects:@"currentGame", @"currentGameType", ..., nil],
    @"player1",

    [NSArray arrayWithObjects:@"currentGame", @"currentGameType", ..., nil],
    @"player2",

    ..., nil];

Second point: Taking this one step further, I think what you really want is another complete dictionary for each player:

NSMutableDictionary * playerInfo;
playerInfo = [NSMutableDictionary dictionaryWithCapcity:0];

[playerInfo setObject:@"1" forKey:@"currentGame"];
[playerInfo setObject:@"2" forKey:@"currentGameType"];
[playerInfo setObject:@"what's up?" forKey:@"currentGameQuestion"];
...

NSMutableDictionary * allPlayers;
allPlayers = [NSMutableDictionary dictionaryWithCapcity:0];

NSInteger playerNum;
for (playerNum = 1; playerNum <= 6; playerNum++) {
    NSString * key = [NSString stringWithFormat:@"Player%d", playerNum];
    [allPlayers setObject:playerInfo forKey:key];
}

Now, after saving and reading the allPlayers dictionary, you would access a single record as follows:

NSDictionary * player2Info = [readDict objectForKey:@"player2"];
NSLog(@"%@", player2Info); // show all keys and values for player 2
NSLog(@"%@", [player2Info valueForKey:@"currentGame"]; // show a single value

Third point: If you are storing a game state for several users, the proper way to do it (from an OO point of view) is to create a Player class and then load and save instances of this class. At a glance, it would look like this:

@interface Player {
    NSString * currentGame;
    NSString * currentGameType;
    NSString * currentGameQuestion;
    ...
}
@property (nonatomic, copy) NSString * currentGame;
@property (nonatomic, copy) NSString * currentGameType;
@property (nonatomic, copy) NSString * currentGameQuestion;
...
- (void)loadFromDictionary:(NSDictionary *)dict;
- (NSDictionary *)saveAsDictionary;
@end

@implementation Player
@synthesize currentGame;
@synthesize currentGameType;
@synthesize currentGameQuestion;
...

- (void)loadFromDictionary:(NSDictionary *)dict {
    [self setCurrentGame:[dict objectForKey:@"currentGame"]];
    [self setCurrentGameType:[dict objectForKey:@"currentGameType"]];
    [self setCurrentGameQuestion:[dict objectForKey:@"currentGameQuestion"]];
    ...
}

- (NSDictionary *)saveAsDictionary {
    return [NSDictionary dictionaryWithObjectsAndKeys:
        currentGame, @"currentGame";
        currentGameType, @"currentGameType";
        currentGameQuestion, @"currentGameQuestion";
        ...
    nil];
}

@end

Now, you can load and save players as follows:

Player * player1 = [[[Player alloc] init] autorelease];
Player * player2 = [[[Player alloc] init] autorelease];
...

NSDictionary * players = [NSDictionary dictionaryWithObjectsAndKeys:
    [player1 saveAsDictionary], @"Player1",
    [player2 saveAsDictionary], @"Player2",
    ...
    nil];

// save dictionary
[players writeToFile:...

// load dictionary
NSDictionary * readDict = ...

[player1 loadFromDictionary:[readDict objectForKey:@"Player1"]];
[player2 loadFromDictionary:[readDict objectForKey:@"Player2"]];
...

See also:

Nested arrays in Objective-C ( NSMutableArray ) (player objects)
What's the best way to store and retrieve multi-dimensional NSMutableArrays? (property list serialization)

Eiko

Well, your dictionary doesn't contain @"Player6", just 1-3.

And @"Player6" is your key, so you want the object not the key!

for (NSArray *object in [dict objectForKey:@"Player1"]) {
    NSLog(@"Value: %@", object);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!