在 NSArray 中存储不同的数据类型

发布于 2024-09-26 03:24:34 字数 1504 浏览 1 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-10-03 03:24:34

哎呀!你有各种各样的问题。

您需要回到绘图板并阅读一些内容。

  1. 如果使用 stringWithFormat,则必须传入格式字符串,该字符串几乎总是字符串文字。如果您作为参数传递的变量包含百分比字符,您的程序将会崩溃。

  2. char 是整数类型,因此您应该将其存储为 NSNumber,或者使用便捷方法:-[NSUserDefaults setInteger:forKey:]。

  3. userDefaults = gameDict 没有按照您的想法行事。 userDefaults 是一个指针和一个局部变量。它不分配用户默认值。您需要了解指针和 C 语言。

  4. 您收到的警告将告诉您问题所在。确保您理解它们。

  5. 我不明白为什么你要初始化字典并使用一些值,然后尝试重新设置它们。无论如何,要对字典进行更改,它必须是可变的,即 NSMutableDictionary 的实例。

  6. 它应该是“gameLoopSpeed”而不是“theGameLoopSpeed”。

应该看起来像这样:

NSUserDefaults *dflts = [NSUserDefaults standardUserDefaults];

[dflts setInteger:score forKey:@"gameScore"];
[dflts setObject:sound forKey:@"gameSound"];
[dflts setFloat:gameLoopSpeed forKey:@"gameLoopSpeed"];
…

如果您的程序很快就会异常终止,那么您只需要同步调用,根据您的情况,从我刚刚看到的情况来看,可能会。 ;-)

Yikes! You've got all kinds of problems.

You need to go back to the drawing board and read up on some stuff.

  1. 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.

  2. A char is an integer type so you should be storing it as an NSNumber, or use the convenience method: -[NSUserDefaults setInteger:forKey:].

  3. 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.

  4. The warnings that you are getting will tell you what the problems are. Make sure you understand them.

  5. 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.

  6. It should be "gameLoopSpeed" not "theGameLoopSpeed".

It should look something like:

NSUserDefaults *dflts = [NSUserDefaults standardUserDefaults];

[dflts setInteger:score forKey:@"gameScore"];
[dflts setObject:sound forKey:@"gameSound"];
[dflts setFloat:gameLoopSpeed forKey:@"gameLoopSpeed"];
…

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. ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文