NSDictionary read data

Now I tried to get this to work for hours, but just can't figure it out.

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 are just part of the tests I'm trying to extract from the dictionary, and this is one of the many ways I tested.

My question is: is there anyone good who can show me how to extract the record (key + objects) and NSLog?

Greetings

+3
source share
2 answers

If you are trying to maintain the state of the game for several players, you may go wrong. I do not want to interfere with your current progress, but I would like to point out some opportunities for improvement.

: NSDictionary (, , plists) , :

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

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

    ..., nil];

. , , , :

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

[playerInfo setObject:@"1" forKey:@"currentGame"];
[playerInfo setObject:@"2" forKey:@"currentGameType"];
[playerInfo setObject:@"what 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, :

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, . :

@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

:

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? ( )

+6

, @"Player6", 1-3.

@"Player6" - , , !

for (NSArray *object in [dict objectForKey:@"Player1"]) {
    NSLog(@"Value: %@", object);
}
+3

Source: https://habr.com/ru/post/1774809/


All Articles