一个实用的NSUserDefault工具类
<h3>前言</h3>
NSUserDefaults类提供了一个与默认系统进行交互的编程接口。NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等。同时NSUserDefaults的缓存避免了在每次读取数据时候都打开用户默认数据库的操作。可以通过调用synchronize方法来使内存中的缓存与用户默认系统进行同步
<h3>原理</h3>
NsUserDefault是用户轻量级的数据持久化,主要用于保存用户程序的配置等信息,以便下次启动程序后能恢复上次的设置。因为是写到了硬盘上,所以会出现用多了会造成系统浪费,内存占用过大等问题
<h3> NSUserDefault 4种情况</h3>
(1)该数据实际上是以“键值对”形式保存的(类似于NSDictionary),因此我们需要通过key来读取或者保存数据(value)。
(2)NSUserDefaults类提供了非常方便的方法来获取常用的类型,例如float,double,interger,Boolean,URL
(3)从NSUserDefaults返回的值是不可改变的,即便是你在存储的时候使用的是可变的值
(4)NSUserDefaults是单例,同时也是线程安全的
<h3>代码实现</h3>
保存一个Boolean类型的
<pre> [NSUserDefaultTools setBooleanValueWithKey:YES key:@"saveBoolean"];
NSLog(@"取出Boolean值========%d",[NSUserDefaultTools getBooleanValueWithKey:@"saveBoolean"]);</pre>
保存一个字符串
<pre>[NSUserDefaultTools setStringValueWithKey:@"哈哈哈" key:@"saveString"];
NSLog(@"取出字符串========%@",[NSUserDefaultTools getStringValueWithKey:@"saveString"]);</pre>
保存一个字典
<pre> [NSUserDefaultTools setUserInfo:@{@"username":@"12345",@"psd":@"1234"}];
NSLog(@"取出字典=======%@",[NSUserDefaultTools getUserInfo]);</pre>
保存一个NSArray
<pre>
NSArray *arr1 = [[NSArray alloc]initWithObjects:@"0",@"5",nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr1];
[NSUserDefaultTools setDataWithKey:data key:@"saveArray"];
NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData: [NSUserDefaultTools getDataWithKey:@"saveArray"]];
NSLog(@"取出字典=======%@",arr2); </pre>
保存一个自定义对象
<pre>
LoginInfo *userinfo=[[LoginInfo alloc]init];
userinfo.resMsg=@"呵呵呵"
userinfo.sign=@"哈哈";
NSData *Logindata = [NSKeyedArchiver archivedDataWithRootObject:userinfo];
[NSUserDefaultTools setDataWithKey:Logindata key:@"saveUserinfo"];
LoginInfo *info = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaultTools getDataWithKey:@"saveUserinfo"]];
NSLog(@"取出自定义对象=======%@",info);</pre>
<h3>具体的核心代码</h3>
NSUserDefaultTools类h文件里面
<pre>
import <Foundation/Foundation.h>
@interface NSUserDefaultTools : NSObject
-
(void)setBooleanValueWithKey:(BOOL)value key:(NSString *)key;
-
(BOOL)getBooleanValueWithKey:(NSString *)key;
-
(void)setStringValueWithKey:(NSString *)value key:(NSString *)key;
-
(NSString *)getStringValueWithKey:(NSString *)key;
-
(void)setUserInfo:(NSDictionary *)userInfo;
/**
返回值为空时,可以看做未登录
*/
- (NSDictionary *)getUserInfo;
- (void)setDataWithKey:(NSData *)data key:(NSString *)key;
- (NSData *)getDataWithKey:(NSString *)key;
@end
</pre>
NSUserDefaultTools类m文件里面
<pre>
-
(void)setStringValueWithKey:(NSString *)value key:(NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:value forKey:key];
[defaults synchronize];
} -
(NSString *)getStringValueWithKey:(NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults objectForKey:key];
} -
(void)setBooleanValueWithKey:(BOOL)value key:(NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:value forKey:key];
[defaults synchronize];
} -
(BOOL)getBooleanValueWithKey:(NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:key];
} -
(void)setUserInfo:(NSDictionary *)userInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[userInfo JSONString] forKey:@"userInfo"];
[defaults synchronize];
} -
(NSDictionary *)getUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userInfoStr = [defaults stringForKey:@"userInfo"];
NSDictionary *userInfo = [userInfoStr objectFromJSONString];
if (userInfo != nil && [userInfo isKindOfClass:[NSDictionary class]]) {
return userInfo;
}
return nil;
} -
(void)setDataWithKey:(NSData *)data key:(NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:data forKey:key];
[defaults synchronize];
} -
(NSData *)getDataWithKey:(NSString *)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults objectForKey:key];
}</pre>
<h3>注意</h3>
保存自定义的对应一定要序列化
Github地址:https://github.com/jwh1650715313/NSUserDefaultTools