NSUserDefaults objectForKey有时为零 [英] NSUserDefaults objectForKey sometimes nil

查看:158
本文介绍了NSUserDefaults objectForKey有时为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚注意到我的 ** NSUserDefaults **
中存在的密钥经常返回nil。似乎大约一半的时间是
正确而另一半则不是。我说有一半的时间,但我不是b $ b意味着它的翻转,只有40-50%,我觉得它不起作用。
当我写初始值时,我立即调用synchronize。
我使用此密钥作为新用户注册时设置的应用程序
修订版。

I've just noticed that a key that exists in my **NSUserDefaults** is returning nil quite often. It seems about half the time it's correct and the other half it's not. I say half the time, but I dont mean that its flip-flopping, its just 40-50% I see it not work. When I write the initial value, I call synchronize immediately. I use this key as the applications revision I set when a new user signs up.

以下代码返回nil:

#define kDBrevision  @"revision"

NSString *rev = [[NSUserDefaults standardUserDefaults] objectForKey:kDBrevision];

当我启动应用程序并只监控值(没有
写任何NSUserDefaults)时,该值有时是有效的
而根本没有修改NSUserDefaults。

When I launch the app and just monitor the value (without writing any NSUserDefaults), the value sometimes is valid with no modifications to the NSUserDefaults at all.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString *r = [[NSUserDefaults standardUserDefaults] objectForKey:kDBrevision];
    NSLog(@"revision %@", r);
    _exit(1);

我不知道为什么会这样。我在连接到Xcode 8.2.1的
设备上运行iOS 10。有人有主意吗?
谢谢

I have no idea why this is happening. Im running iOS 10 on my device connected to Xcode 8.2.1. Anyone have any ideas? Thanks

编辑:
我开始和苹果谈论修复此问题并发现如果你有
文件保护已完全打开,这可能是这个问题的原因
不时出现,但苹果告诉我,我特别的
案例(这是我此时唯一确定的)是一个错误。
案例是当你使用Xcode在设备上启动应用程序时它应该是
不会像这样失败而且它偶尔也会失败。不知道何时或是否将修复
。相反,我将我的关键字符串从默认值移到了
的钥匙串。

I started talking to apple about fixing this and found out that if you have file protection complete turned on, it can be the cause of this issue showing up from time to time, however apple told me that my particular case (which is the only one I was sure of at this time) is a bug. The case is when you use Xcode to launch the app on the device it should not fail like this and it occasionally does. No idea when or if it will be fixed. Instead I moved my critical strings from the defaults to the keychain instead.

推荐答案

这似乎是一个Xcode 8和/或iOS 10错误。我自己遇到了这个问题并最终将案例缩小到UserDefaults间歇性地返回nil,而实际上有关键的数据。具体来说,我设置数据一次,然后删除设置逻辑并重复执行get逻辑,有时得到值,有时得到nil。

This appears to be an Xcode 8 and/or iOS 10 bug. I ran into it myself and conclusively narrowed the case to UserDefaults intermittently returning nil when in fact there was data for the key. Specifically, I set the data once, then removed the set logic and executed only the get logic, repeatedly, and sometimes got values, sometimes got nil.

我改变了我的运行使用Release配置的方案并在没有连接调试器的情况下在设备上运行app,问题就消失了,也就是说,每次执行时,get逻辑都会生成正确的值(~30次)。

I changed my Run scheme to use Release configuration and ran the app on device without the debugger attached and the issue disappeared, that is, the get logic produced the correct value every time I executed it (~30 times).

以下是一些讨论:

iOS 10与XCode 8 GM导致NSUserDefaults间歇性无法正常工作

iOS 10,NSUserDefaults不起作用

https://forums.developer.apple.com/thread/48700

https://forums.developer.apple.com/message/143155#143155

这篇关于NSUserDefaults objectForKey有时为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆