xcode 从设置包中获取(默认)值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9025676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:06:41  来源:igfitidea点击:

Getting (default) value from settings bundle

objective-cxcodensuserdefaultssettings.bundle

提问by Tom

I have an IOS5 project.

我有一个 IOS5 项目。

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

我在我的项目中添加了一个设置包,并为其添加了一些设置。

I set properties of 'host_ip':

我设置了“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>

I try to read value like this:

我尝试读取这样的值:

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

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

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

How can I get my default value?

如何获得我的默认值?

UPDATE

更新

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.

用户编辑设置后,我可以通过上面的代码检索它。我的问题是获取设置值,如果用户还没有编辑它。我认为在这种情况下它应该返回我在 plist 中设置的默认值。

采纳答案by bbum

Did you register the defaults via NSUserDefaults?

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

See the registerDefaults:method on NSUserDefaults.

请参阅NSUserDefaults 上registerDefaults:方法。