NSDictionary读取数据 [英] NSDictionary read data
问题描述
我现在试着让它工作几个小时,但是无法正常工作。
I have now tried to get this to work for a few hours but just cannot get it right.
我有以下代码:
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);
}
for循环只是尝试提取数据的测试的一部分来自字典,是我测试的许多不同方式之一。
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.
我的问题是,是否有人可以告诉我如何提取记录(键+对象) )和NSLog吗?
My question is if there is someone nice who can show me how to extract a record (key + objects) and NSLog it?
干杯
推荐答案
如果你正在尝试为了保存一些玩家的游戏状态,你可能会采取错误的方式。我不想阻碍你目前的进展,但我想指出一些改进途径。
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.
第一点: NSDictionary
对象(以及plists,就此而言)可以将数据存储在树结构中,因此不需要为每个用户提供不同的密钥集: / p>
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];
}
现在,保存并阅读 allPlayers
字典,你可以按如下方式访问单个记录:
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
第三点:如果您要为多个用户存储游戏状态,正确的方式可以它(从OO的角度来看)是创建一个 Player
类,然后加载并保存这个类的实例。一目了然,它看起来像这样:
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"]];
...
参见:
Objective-C中的嵌套数组(NSMutableArray)(播放器对象)
存储和检索多维NSMutableArrays的最佳方法是什么?(属性列表序列化)
Nested arrays in Objective-C ( NSMutableArray ) (player objects)
What's the best way to store and retrieve multi-dimensional NSMutableArrays? (property list serialization)
这篇关于NSDictionary读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!