NSUserDefaults - 保存数据以供使用和重用 - 每次新启动时都会擦除?
因此,对于我的游戏,我保存用户是否已解锁加电的“bool”值。为此,我使用 NSUserDefaults。我最初遵循了有关 NSUserDefaults 的在线教程,所以让我放置我的代码并进行解释(以下所有代码都在 AppDelegate.m 文件中):(
注意:“save”是一个宏:[NSUserDefaults standardUserDefaults]
)
powerups = [[NSMutableArray alloc] init];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"dateKey"] == nil) {
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate date], @"dateKey", nil];
[save setBool:NO forKey:POWER_1];
[save setBool:NO forKey:POWER_2];
[save setBool:NO forKey:POWER_3];
[save setBool:NO forKey:POWER_4];
[save setBool:NO forKey:POWER_5];
[save setBool:NO forKey:POWER_6];
[save setBool:NO forKey:POWER_7];
[save setBool:NO forKey:POWER_8];
[save setBool:NO forKey:POWER_9];
[save setInteger:0 forKey:@"1HighScore"];
[save setInteger:0 forKey:@"2HighScore"];
[save setInteger:0 forKey:@"3HighScore"];
[save setInteger:0 forKey:@"CumulativeScore"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"firstrun");
}
因此,在上面的代码中,我创建了一个数组,可以从游戏中的任何类调用该数组,该数组存储所有可用的电源。仅当这是第一次玩游戏时才应调用此“if”语句,因为它会在 NSUserDefaults 中创建所有数据值。注意:我使用越狱的 iPhone 并检查了创建的 .plist 文件。似乎没有任何所谓的/具有“dateKey”值的值。该教程说,这是检查这是否是第一次运行的方法,因为如果日期值不存在,那么它是第一次运行等。从逻辑上讲,这是有道理的。
现在,下面的代码填充数组,前提是 NSUserDefaults 必须存在,无论它是刚刚“填充”还是之前保存:
if([save boolForKey:POWER_1])
[powerups addObject:POWER_1];
if([save boolForKey:POWER_2])
[powerups addObject:POWER_2];
if([save boolForKey:POWER_3])
[powerups addObject:POWER_3];
if([save boolForKey:POWER_4])
[powerups addObject:POWER_4];
if([save boolForKey:POWER_5])
[powerups addObject:POWER_5];
if([save boolForKey:POWER_6])
[powerups addObject:POWER_6];
if([save boolForKey:POWER_7])
[powerups addObject:POWER_7];
/*if([save boolForKey:POWER_8])
[powerups addObject:POWER_8];*/
if([save boolForKey:POWER_9])
[powerups addObject:POWER_9];
现在,似乎在每次新运行时,第一个“if”语句都是 YES,并且所有的值根据第一个代码框重置...然后导致第二个代码框做出相应的操作并且没有数组填充,因为所有内容都已重置...
这可能与 registerDefaults 有关,是否有必要致电或使用,因为我很确定这就是问题所在?
谢谢大家,希望我说清楚了:)
rnc505
So for my game, I'm saving the "bool" value of whether or not the user has unlocked a powerup yet. For this I am using NSUserDefaults. I had originally followed a tutorial online about NSUserDefaults so let me put my code and explain (all of the following code is in the AppDelegate.m file):
(NOTE: "save" is a macro for: [NSUserDefaults standardUserDefaults]
)
powerups = [[NSMutableArray alloc] init];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"dateKey"] == nil) {
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate date], @"dateKey", nil];
[save setBool:NO forKey:POWER_1];
[save setBool:NO forKey:POWER_2];
[save setBool:NO forKey:POWER_3];
[save setBool:NO forKey:POWER_4];
[save setBool:NO forKey:POWER_5];
[save setBool:NO forKey:POWER_6];
[save setBool:NO forKey:POWER_7];
[save setBool:NO forKey:POWER_8];
[save setBool:NO forKey:POWER_9];
[save setInteger:0 forKey:@"1HighScore"];
[save setInteger:0 forKey:@"2HighScore"];
[save setInteger:0 forKey:@"3HighScore"];
[save setInteger:0 forKey:@"CumulativeScore"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"firstrun");
}
So in the above code, I am creating an array, which can be called from any class in my game, that stores all of the available powerups. This "if" statement should only be called if this is the first time the game is played, because it creates all of the data values in the NSUserDefaults. NOTE: I use a jailbroken iPhone and have inspected the created .plist file. There seems to not be any value whatsoever called/with the value of "dateKey". The tutorial said that this is the way to check if this is the first run, because if a date value doesn't exist then its the first run etc. Logically, it made sense.
Now, the following code, populates the array given that the NSUserDefaults MUST exist, whether or not it was just "populated" or saved previously:
if([save boolForKey:POWER_1])
[powerups addObject:POWER_1];
if([save boolForKey:POWER_2])
[powerups addObject:POWER_2];
if([save boolForKey:POWER_3])
[powerups addObject:POWER_3];
if([save boolForKey:POWER_4])
[powerups addObject:POWER_4];
if([save boolForKey:POWER_5])
[powerups addObject:POWER_5];
if([save boolForKey:POWER_6])
[powerups addObject:POWER_6];
if([save boolForKey:POWER_7])
[powerups addObject:POWER_7];
/*if([save boolForKey:POWER_8])
[powerups addObject:POWER_8];*/
if([save boolForKey:POWER_9])
[powerups addObject:POWER_9];
Now, it seems that on EVERY new run, the first "if" statement is YES, and all of the values are reset according that the first box of code... which then causes the second box of code to act accordingly and have no array population because everything has been reset...
Could this have something to do with registerDefaults
, is it even necessary to call or use, because I'm pretty sure this is the issue?
Thanks everyone, hopefully I was clear :)
rnc505
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 registerDefaults 没有根据 Apple 文档写入磁盘
您有 2 个选择,1)不要使用 registerDefaults 并像这样保存您的“日期”
这将导致它被写入磁盘并且您的代码将起作用。
更可靠的方法是将所有的 powerups 写入 registerDefaults 并删除日期。为了确保在执行其他操作之前调用 registerDefaults,请将其放入 appDelegate 的初始化方法中,如下所示:
The problem is registerDefaults is not written to disk per the Apple Docs
You have 2 options, 1) don't use registerDefaults and save your "date" like this
This will cause it to be written to disk and your code will work.
The more robust way would be to write all your powerups to registerDefaults and do away with the date. To ensure that the registerDefaults is called before anything else put it in the initialize method of your appDelegate, like this: