NSUserdefaults的基本应用
程序员文章站
2024-01-21 10:42:40
...
简介
NSUserDefaults是一个单例,在整个程序中只有一个实例对象,他可以用于数据的永久保存,而且简单实用。适合存储轻量级的本地数据。
支持的数据类型:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL
注意:对相同的Key赋值约等于一次覆盖,故要保证每一个Key的唯一性
存取简单数据到本地
- 取
NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
NSData *data = [userD objectForKey:@"UUID"];
- 存
[userD setObject:uuidString forKey:@"UUID"];
[userD synchronize]; //同步,确保保存成功
存取图片
- 存
UIImage *img3;
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(img3) forKey:@"sharePic3"];
[[NSUserDefaults standardUserDefaults] synchronize];
- 取
NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
NSData* imageData = [userD objectForKey:@"sharePic1"];
UIImage *img = [UIImage imageWithData:imageData];
存取字典
存取的字典对象都必须是不可变的
-存
// 新增的字典
NSDictionary *dicAdd = @{@"key" : @"value"};
// 添加
[cur_dict addEntriesFromDictionary:dicAdd];
// 先化为不可变字典,再保存
NSDictionary *fdict =[[NSDictionary alloc] initWithDictionary:cur_dict];
[[NSUserDefaults standardUserDefaults] setObject:fdict forKey:@"Adict"];
-取
// 取出
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"Adict"];
// 化为可变字典 进行后续操作
NSMutableDictionary *cur_dict = [[NSMutableDictionary alloc] initWithCapacity:0];
[cur_dict addEntriesFromDictionary:dict];
存取数组
存取的数组对象都必须是不可变的
- 存
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"123",@"234", nil];
// 先转换为不可变 数组
NSArray * array = [NSArray arrayWithArray:mutableArray];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:array forKey:@"names"];
- 取
NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
NSArray *arr = [userD arrayForKey:@"names"];
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:arr];
应用:存储自定义对象
创建一个模型(user)
在.h中
#import <Foundation/Foundation.h>
@interface PDUser : NSObject<NSCoding>
@property(nonatomic,strong)NSString *apiKey;
@property(nonatomic,strong)NSString *apiSecret;
@property(nonatomic,strong)NSString *regDev;
@property(nonatomic,assign)NSInteger user_id;
@end
在.m中
#import "PDUser.h"
@implementation PDUser
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.apiKey forKey:@"apiKey"];
[aCoder encodeObject:self.apiSecret forKey:@"apiSecret"];
[aCoder encodeObject:self.regDev forKey:@"UUID"];
[aCoder encodeObject:[NSString stringWithFormat:@"%ld",self.user_id] forKey:@"userid"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.apiKey = [aDecoder decodeObjectForKey:@"apiKey"];
self.apiSecret = [aDecoder decodeObjectForKey:@"apiSecret"];
self.regDev = [aDecoder decodeObjectForKey:@"UUID"];
self.user_id = [[aDecoder decodeObjectForKey:@"userid"] integerValue];
}
return self;
}
外界使用
保存在userdefault中,以及从中取出user
- 保存
//把user对象存入 userdefault
NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
PDUser *user = [[PDUser alloc] init];
user.apiSecret = _reBackData.apiSecret;
user.apiKey = _reBackData.apiKey;
user.regDev = _reBackData.regDev;
user.user_id = _reBackData.user_id;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
[userD setObject:data forKey:@"USER"]; //保存
- 取出
//从中取出user对象
NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
NSData *data = [userD objectForKey:@"USER"];
PDUser *user = [NSKeyedUnarchiver unarchiveObjectWithData:data];
下一篇: MySQL索引详解