在 NSArray 中存储不同的数据类型
我试图在 NSDictionary 中存储不同的数据类型,以便在游戏终止时保存在 NSUserdefaults 中。我正在尝试存储一个字符、3 个浮点数和一个字符串,我不断收到有关字符和浮点数的警告,但我似乎无法在任何地方找到答案。 1)我还需要设置阵列吗? 2)如何将不同的数据类型存储到数组或字典等对象中?代码如下所示:
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];
I'm trying to store different data types in a NSDictionary to save in NSUserdefaults when the game terminates. I'm trying to store a char, 3 floats and a string, I keep getting a warning on the char and the floats and I cant seem to find the answer anywhere. 1)Do i even need to setup the arrays? 2) How do I store the different data types to an object like an array or dictionary? the code looks like the this:
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀!你有各种各样的问题。
您需要回到绘图板并阅读一些内容。
如果使用 stringWithFormat,则必须传入格式字符串,该字符串几乎总是字符串文字。如果您作为参数传递的变量包含百分比字符,您的程序将会崩溃。
char 是整数类型,因此您应该将其存储为 NSNumber,或者使用便捷方法:-[NSUserDefaults setInteger:forKey:]。
userDefaults = gameDict 没有按照您的想法行事。 userDefaults 是一个指针和一个局部变量。它不分配用户默认值。您需要了解指针和 C 语言。
您收到的警告将告诉您问题所在。确保您理解它们。
我不明白为什么你要初始化字典并使用一些值,然后尝试重新设置它们。无论如何,要对字典进行更改,它必须是可变的,即 NSMutableDictionary 的实例。
它应该是“gameLoopSpeed”而不是“theGameLoopSpeed”。
它应该看起来像这样:
如果您的程序很快就会异常终止,那么您只需要同步调用,根据您的情况,从我刚刚看到的情况来看,可能会。 ;-)
Yikes! You've got all kinds of problems.
You need to go back to the drawing board and read up on some stuff.
If you use stringWithFormat, you must pass in a format string which will almost always be a string literal. If the variable that you're passing as an argument contains percent characters, your program will crash.
A char is an integer type so you should be storing it as an NSNumber, or use the convenience method: -[NSUserDefaults setInteger:forKey:].
userDefaults = gameDict isn't doing what you think it is. userDefaults is a pointer and a local variable. It's not assigning the user defaults. You need to understand pointers and the C language.
The warnings that you are getting will tell you what the problems are. Make sure you understand them.
I don't understand why you're initialising a dictionary and with some values and then trying to set them all over again. In any case, to make changes to a dictionary, it needs to be mutable, i.e. an instance of NSMutableDictionary.
It should be "gameLoopSpeed" not "theGameLoopSpeed".
It should look something like:
And you only need the synchronize call if your program is going to terminate abnormally soon after, which in your case, judging from what I've just seen, probably will. ;-)