Getting (default) value from settings bundle

爷,独闯天下 提交于 2019-12-07 17:15:47

问题


I have an IOS5 project.

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

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>

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.

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.


回答1:


Did you register the defaults via NSUserDefaults?

See the registerDefaults: method on NSUserDefaults.



来源:https://stackoverflow.com/questions/9025676/getting-default-value-from-settings-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!