我正在创建一个 userDefault bool 键,该键在我尝试更改键值之前一直有效。更改密钥的值后,密钥似乎从应用程序包中消失了。例如,以下代码在我的 GameManager 的 init 例程中(它是一个服务于我的游戏应用程序所有级别的单例),并在我的应用程序首次加载时运行:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // get the shared defaults object
if (defaults) {
if ([defaults boolForKey:@"Sound Effects ON"]) {
isSoundEffectsON = [defaults boolForKey:@"Sound Effects ON"];
}
else {
[defaults setBool: YES forKey:@"Sound Effects ON"];
isSoundEffectsON = YES;
[defaults synchronize]; // save the initial state
}
}
首次输入此代码时,执行路径采用 else 子句,因为 @"Sound Effects ON" 的 userDefault 目前不存在。如果我对@"Sound Effects ON" 不做任何其他操作并重新启动应用程序(从而重新输入此代码),则执行路径将采用 if 子句并检索 @"Sound Effects ON" 的存储值(这表明密钥已成功存储在应用程序包中)。
稍后,当我的一个游戏关卡正在运行并且用户决定更改此键的值时,将执行以下代码:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // get the shared defaults object
if ([[GameManager sharedGameManager] isSoundEffectsON]) {
[[GameManager sharedGameManager] setIsSoundEffectsON:NO];
if (defaults) {
// Update the state for sound effects
[defaults setBool: NO forKey:@"Sound Effects ON"];
[defaults synchronize]; // save the current state
}
} else {
[[GameManager sharedGameManager] setIsSoundEffectsON:YES];
if (defaults) {
// Update the state for sound effects
[defaults setBool: YES forKey:@"Sound Effects ON"];
[defaults synchronize]; // save the current state
}
}
如果 isSoundEffects 之前已打开,则 @"Sound Effects ON" bool 设置为 NO,并且 userDefault 已同步(可能是为了保存值)。采用此执行路径时,似乎会从应用程序包中删除此密钥的 userDefault。我之所以这么说是因为当重新加载应用程序并且 GameManager init 中的代码再次执行时,else 子句运行表明@“Sound Effects ON”没有 userDefault 键。
我错过了什么?为什么这把钥匙不见了?