从设置包获取(默认)值 [英] Getting (default) value from settings bundle

查看:87
本文介绍了从设置包获取(默认)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IOS5项目.

I have an IOS5 project.

我已将设置包添加到项目中,并添加了一些设置.

I have added a settings bundle to my project and added some settings to it.

我设置了"host_ip"的属性:

I set properties of 'host_ip':

<dict>
        <key>AutocapitalizationType</key>
        <string>None</string>
        <key>AutocorrectionType</key>
        <string>No</string>
        <key>DefaultValue</key>
        <string>http://localhost</string>
        <key>IsSecure</key>
        <false/>
        <key>Key</key>
        <string>host_ip</string>
        <key>KeyboardType</key>
        <string>URL</string>
        <key>Title</key>
        <string>Host</string>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
    </dict>

我尝试读取这样的值:

NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
NSString *host = [userDefaults stringForKey:@"host_ip"];
NSLog(@"%@",host);

它不返回我在host_ip中设置的默认值,而是返回nil.

It does not return the default value I set in host_ip, it returns nil.

如何获取默认值?

更新

用户编辑设置后,可以通过上面的代码进行检索.我的问题是获取设置值(如果尚未由用户编辑).在这种情况下,它应该返回我在plist中设置的默认值.

After a user edits a setting, I can retrieve it by the code above. My problem is getting the setting value, if it was not edited by the user yet. As I think in that case it should return the default I set in plist.

推荐答案

您是否通过NSUserDefaults注册了默认值?

Did you register the defaults via NSUserDefaults?

请参见

See the registerDefaults: method on NSUserDefaults.

这篇关于从设置包获取(默认)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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