iPhone-阅读和保存首选项 [英] iPhone - reading and saving preferences

查看:67
本文介绍了iPhone-阅读和保存首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 Apple文档关于首选项,但这对我来说仍然有点复杂.我有一个带有用于设置首选项的自定义屏幕的应用程序,我只需要用于管理读写内容的代码.

I've read the Apple doc about Preferences but this is still a little bit complex for me to understand. I have an application with a custom screen for setting the Preferences, and I'd like just the code to manage the read and write stuff.

您是否知道详细的教程(几年前未写过)或我可以阅读以了解的项目样本代码?

Would you know a detailed tutorial (not writen years ago) or a project sample code somewhere I could read to understand ?

推荐答案

您应使用NSUserDefaults:

You should use NSUserDefaults :

您这样设置:

       NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

然后您可以像这样设置新对象:

then you can set new objects like that:

   [defaults setBool:YES forKey:@"bools"];
   [defaults setObject:[NSNumber numberWithInt:14] forKey:@"numbers"];
   [defaults setFloat:60.0 forKey:@"floats"];
   [defaults setObject:@"simple string" forKey:@"strings"];
   [defaults setObject:[NSDate date]  forKey:@"dates"];

当您需要读取值时,可以使用:

when you need to read a value you can use :

   NSUInteger integerFromPrefs = [defaults integerForKey:@"integers"];
   BOOL boolFromPrefs = [defaults boolForKey:@"bools"];
       NSString *stringFromPrefs = [defaults objectForKey:@"bools"];
       etc...

,并记住在每次更改后同步您的更改:

and remember to synchronize your changes after each change:

   [defaults synchronize];

顺便说一句

您可以从应用程序中的任何视图读取和写入NSUserDefaults.

You can read and write to the NSUserDefaults from any view in your application.

修改

要查看NSUserDefaults中的所有数据,可以使用:

To see all of the data in the NSUserDefaults you can use:

  NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

这将打印存储在plist中的所有键和值.

This will print all the keys and values stored in the plist.

祝你好运

这篇关于iPhone-阅读和保存首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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