gpt4 book ai didi

iphone - NSUserDefaults boolForKey 出现意外结果

转载 作者:行者123 更新时间:2023-12-03 19:22:59 25 4
gpt4 key购买 nike

从设备完全卸载应用程序并将其加载到调试器中后,我尝试在设置方法中使用 boolForKey 加载标志。应用程序第一次运行时,我预计 bool 将不存在,因为我刚刚重新安装了应用程序。我从文档中预计 boolForKey 将返回 NO。

但我看到的恰恰相反。 boolForKey 返回 YES,这符合我的初始用户设置。知道为什么会发生这种情况或者有好的解决方法吗?

BOOL stopAutoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"StopAutoLogin"];
_userWantsAutoLogin = !stopAutoLogin;

因此 stopAutoLogin 显示为“YES”,这完全出乎意料。

陌生人和陌生人:当我调用 objectForKey:@"StopAutoLogin"时,我得到了一个 nil 对象,正如预期的那样。只是 boolForKey 返回了错误值。所以我将代码更改为:

// this is nil
NSObject *wrapper = [[NSUserDefaults standardUserDefaults] objectForKey:@"StopAutoLogin"];

// this is YES
BOOL stopAutoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"StopAutoLogin"];

最佳答案

请尝试[UserDefaults 同步];

由于此方法会定期自动调用,因此仅当您无法等待自动同步(例如,如果您的应用程序即将退出)或者您想要将用户默认值更新为当前状态时,才使用此方法磁盘,即使您没有进行任何更改。

请参阅:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

关于iphone - NSUserDefaults boolForKey 出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com