3

I have now tried to get this to work for a few hours but just cannot get it right.

I have the following code:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:  

[NSArray arrayWithObjects:@"currentGame1", @"currentGameType1", @"currentGameQuestion1", @"currentGameRightAnswers1", @"currentGameType1", @"numberOfType0Games1", @"type0Results1", @"numberOfType1Games1", @"type1Results1",@"numberOfType2Games1", @"type2Results1",nil], @"Player1",  

[NSArray arrayWithObjects:@"currentGame2", @"currentGameType2", @"currentGameQuestion2", @"currentGameRightAnswers2", @"currentGameType2", @"numberOfType0Games2", @"type0Results2", @"numberOfType1Games2", @"type1Results2",@"numberOfType2Games2", @"type2Results2",nil], @"Player2",  

[NSArray arrayWithObjects:@"currentGame3", @"currentGameType3", @"currentGameQuestion3", @"currentGameRightAnswers3", @"currentGameType3", @"numberOfType0Games3", @"type0Results3", @"numberOfType1Games3", @"type1Results3",@"numberOfType2Games3", @"type2Results3",nil], @"Player3",nil];  
[dict writeToFile:@"/Users/MikaelB/Desktop/xxxxPlayer.plist" atomically: TRUE];  

NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/Users/MikaelB/Desktop/xxxxPlayer.plist"];  

NSLog(@"readDict: %@", readDict);  
NSLog(@"= = = = = = = = = = = = = = = = = = =");

for (NSArray *key in [readDict allKeysForObject:@"Player6"]) {  
    NSLog(@"Key: %@", key);  
}  

The for loops is just part of the tests i do to try to extract data from the dictionary and is one of many different ways i have tested.

My question is if there is someone nice who can show me how to extract a record (key + objects) and NSLog it?

Cheers

Anurag
  • 140,337
  • 36
  • 221
  • 257
PeterK
  • 4,243
  • 4
  • 44
  • 74
  • Is this for iPhone? What are you trying to do with /Users/MikaelB/Desktop/xxxxPlayer.plist? Or the question is wrongly tagged ihpone? – taskinoor Nov 14 '10 at 18:38
  • I guess it was for debugging reasons on the simulator. – Eiko Nov 14 '10 at 18:41
  • Yes this is for iPhone. The path is just for testing when i try to get this to work. When it works i will implement it into my app i am working on. – PeterK Nov 14 '10 at 18:48

2 Answers2

6

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)

Community
  • 1
  • 1
e.James
  • 116,942
  • 41
  • 177
  • 214
3

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);
}
e.James
  • 116,942
  • 41
  • 177
  • 214
Eiko
  • 25,601
  • 15
  • 56
  • 71