从设置包中获取(默认)值
我有一个IOS5项目。
我已向我的项目添加了一个设置包,并向其中添加了一些设置。
我设置“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。
我怎样才能得到我的默认值?
更新
用户编辑设置后,我可以通过上面的代码检索它。我的问题是获取设置值(如果用户尚未编辑它)。我认为在这种情况下它应该返回我在 plist 中设置的默认值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否通过 NSUserDefaults 注册了默认值?
请参阅
registerDefaults:
方法NSUserDefaults。Did you register the defaults via
NSUserDefaults
?See the
registerDefaults:
method on NSUserDefaults.