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);
NSLog(@"%@", [player2Info valueForKey:@"currentGame"];
. , ( 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];
[players writeToFile:...
NSDictionary * readDict = ...
[player1 loadFromDictionary:[readDict objectForKey:@"Player1"]];
[player2 loadFromDictionary:[readDict objectForKey:@"Player2"]];
...
. :
Objective-C (NSMutableArray) ( )
NSMutableArrays? ( )