从standardUserDefaults获得nil, [英] Getting nil from standardUserDefaults,

查看:182
本文介绍了从standardUserDefaults获得nil,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序创建了一个Settings.bundle,我尝试通过以下代码读取设置:

I have created a Settings.bundle for my application, and I try to read the settings by the following code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(!defaults)
{
    NSLog("can not get default bundle");
}
NSString *usr = [NSString stringWithFormat:@"%@",[defaults stringForKey:@"username"]];
NSLog(usr);

控制台中的输出为(null)。我之前使用过它并且它起作用了。这种情况发生在我升级到iOS的最新xcode之后。我不确定是否有任何变化?

The output in the console is (null). I have used this before and it worked. This situation happened after I upgraded to the latest xcode for iOS. I am not sure if there is any change?

以下是我的Settings.bundle拍摄的屏幕。

Here is the screen shot from my Settings.bundle.

推荐答案

stringForKey:返回 nil

它返回 nil 因为找不到用户名的密钥。

It's returning nil because it can't find a key for "username".

找不到用户名的密钥,因为该密钥在该应用的用户默认值中不存在。

It can't find a key for "username" because the key doesn't exist in the user defaults for that app.

密钥不存在,因为(A)它从未存在过,对于该应用程序,或(B)它已存在但已删除。

The key doesn't exist because either (A) it never existed, for that app, or (B) it existed but it was deleted.

(A)如果应用程序通过Springboard删除,可能会在设备或模拟器上发生,或者如果整个设备(模拟器)被重置。这两种情况中的任何一种都是真的吗?

(A) Can happen on the device or simulator if the app gets deleted via Springboard, or if the entire device (Simulator) gets reset. Are either of those two scenarios true?

(B)如果你在代码中做了一些删除密钥的事情,就会发生这种情况。你能否证实情况并非如此?

(B) Can happen if you'd done something in your code to delete the key. Can you confirm that is not the case?

这篇关于从standardUserDefaults获得nil,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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