现在的位置: 首页 > 综合 > 正文

ios开发之NSUserDefaults

2019年10月01日 ⁄ 综合 ⁄ 共 2643字 ⁄ 字号 评论关闭

1.  什么是 NSUserDefaults?

NSUserDefaults 提供了同默认系统交互的编程接口,默认系统允许应用程序根据用户的偏好进行行为设置。例如,你可以允许用户选择应用程序显示的计量单位是什么,或者文档自动保存的频率。应用程序通过对一系列的参数进行赋值然后存储在用户的默认数据库中来记录诸如此类的偏好。这些参数经常用来决定应用程序启动时的默认状态或者默认操作。

运行时,你使用 NSUserDefaults 对象从用户的默认数据库中读取应用程序所需要的默认值。NSUserDefaults 缓存这些信息从而避免你每次需要默认值的时候都必须去打开用户的默认数据库。synchronize 函数,经过周期性的时间间隔就会被自动调用来保持内存中缓存的数据同用户的默认数据库同步。

NSUserDefauts 提供了一些便利方法来访问诸如 floats, doubles, integers, Booleans, and URLs 的常用数据类型。一个默认对象必须是一个属性列表,也就是 NSData, NSString, NSNumber, NSDate, NSArray, 或者 NSDictionary 的一个实例(对于集合来说是实例的集合)。如果你想要存储其他类型的对象,你通常应该将它们序列化为一个NSData的对象。详细信息看 Preferences
and Settings Programming Guide

从NSUserDefaults 中返回的数据都是不可变的,即使你将设置该值时设置的是可变的对象也是如此。例如你设置一个可变字符串作为 “MyStringDefault” 参数的值,之后你使用 

stringForKey:方法获取的该参数的值也会是不可变的。

一个默认的数据库会自动为每个用户创建,The NSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferences API (seePreferences
Utilities Reference
). However, NSUserDefaults correctly reads per-host preferences, so you can safely mix CFPreferences code with NSUserDefaults code.

如果你的应用程序支持管理环境,你可以通过 NSUserDefaults 对象来决定哪些参数由管理员来为用户进行配置。管理的环境对应于管理员或者教师可能会以一定的方式配置系统的计算机实验室或者教室。在这些情形中,管理员可以进行一系列的偏好设置并且强制用户使用这些偏好设置。如果一个偏好是以这种方式管理的,应用程序应该禁止相应的控制,使用户不能够编辑这个偏好。

NSUserDefaults 类是 线程安全的。

沙盒App不能访问或者编辑任何其它应用程序的偏好设置。例如,如果你使用 addSuiteNamed:加入另外一个应用的域名,你也不能够获取该应用的偏好设置的访问权限。

尝试访问或者编辑其它应用程序的偏好设置不好产生任何错误,但是如果你这样做,OS X 实际上读和写的是你的应用程序容器内的文件,而不是另外一个应用的偏好设置文件。

Persistence of NSURL and file reference URLs
When using NSURL instances to refer to files within a process, it's important to make the distinction between location-based tracking (file: scheme URLs that are basically paths) versus filesystem identity tracking (file: scheme URLs that are file reference
URLs). When persisting an NSURL, you should take that behavior into consideration. If your application tracks the resource being located by its identity so that it can be found if the user moves the file, then you should explicitly write the NSURL's bookmark
data or encode a file reference URL.

If you want to track a file by reference but you require explicit control over when resolution occurs, you should take care to write out bookmark data to NSUserDefaults rather than rely on -[NSUserDefaults setURL:forKey:]. This allows you to call +[NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:]
at a time when you know your application will be able to handle the potential I/O or required user interface interactions.

可采用如下方法设置偏好:

[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                            [NSNumber numberWithBool:YES], @"show_avatar",
                                                            [NSNumber numberWithBool:YES], @"show_pics", nil]];

或者:

[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:NO] forKey:@"showUpdateAlert"];

获取 NSUserDefaults 中的偏好设置:

BOOL showPictures = [[NSUserDefaults standardUserDefaults] boolForKey:@"show_pics"];

抱歉!评论已关闭.