我试图在NSDictionary中存储不同的数据类型,以在游戏终止时保存在NSUserdefaults中。我试图存储一个字符,3个浮点数和一个字符串,我不断收到关于字符和浮点数的警告,我似乎无法在任何地方找到答案。 1)我甚至需要安装阵列? 2)如何将不同的数据类型存储到像数组或字典这样的对象中?该代码看起来像这样:在NSArray中存储不同的数据类型
gameKeys = [[NSArray alloc] initWithObjects:@"gameScore",@"gameSound",@"gameDifficulty",@"theGameLoopSpeed",@"theDelayGameSpeed",nil];
gameValues = [[NSMutableArray alloc] init];
[gameValues setValue:score forKey:@"gameScore"];
[gameValues addObject:[NSString stringWithFormat:score]];// unsigned char
[gameValues addObject:[NSString stringWithFormat:sound]];//string
[gameValues addObject:[NSString stringWithFormat:gameDifficulty]];// char
[gameValues addObject:[NSNumber numberWithFloat:gameLoopSpeed]];// float
[gameValues addObject:[NSNumber numberWithFloat:delayGameLoopSpeed]];//float
NSDictionary *gameDict = [[NSDictionary alloc] initWithObjects:gameKeys forKeys:gameValues];
[gameDict setObject:[NSString stringWithFormat:score] forKey:@"gameScore"];//unsigned char
[gameDict setObject:[NSString stringWithFormat:sound] forKey:@"gameSound"];//string
[gameDict setObject:[NSString stringWithFormat:gameDifficulty] forKey:@"theGameDifficulty"];//char
[gameDict setObject:[NSNumber numberWithFloat:gameLoopSpeed] forKey:@"theGameLoopSpeed"];//float
[gameDict setObject:[NSNumber numberWithFloat:delayGameLoopSpeed] forKey:@"theDelayGameLoopSpeed"];//float
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
userDefaults = gameDict;
[userDefaults synchronize];
警告说什么? – 2010-10-01 06:10:52
@Robot K:我的猜测是它说“非常量格式字符串”。 – JeremyP 2010-10-01 09:01:34
只是。哇!我还没有看到那么多的真实世界代码的编程错误。这段代码应该会在第4行崩溃,因为gameValues是一个“NSMutableArray”,因此它不会响应KVC选择器。 (毕竟,首先应该有一个编号列表中的“gameScore”元素是什么?) – danyowdee 2010-10-01 09:37:50