iOS数据持久化 归档 plist文件
2016-06-07 14:40
260 查看
一般我们工程基本需要做数据持久化的要求,以减少流量的损失和用户没网的体验
下面介绍三种数据持久化的方法
第二种:归档
创建一个类 并且遵守<NSCoding>这个协议
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int age;
@property(nonatomic,assign)double height;
写三个要保存的属性
-(void)encodeWithCoder:(NSCoder *)aCoder 当将一个自定义对象保存到文件的时候就会调用该方法
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{ 当从文件中读取一个对象的时候就会调用该方法
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.height = [aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
Student *stu = [Student new]; 初始化并赋值
stu.name = @"haha";
stu.age = 223232;
stu.height = 34234.532;
stu.weight = 34300002;
NSString *sysPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];获取文件路径并将文件的对象保存到文件中
NSString *path = [sysPath stringByAppendingPathComponent:@"student.lh"];
NSLog(@"%@",path);
[NSKeyedArchiver archiveRootObject:stu toFile:path];
读取数据
NSString *sysPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [sysPath stringByAppendingPathComponent:@"student.lh"];
Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSMutableArray *array = [@[stu.name,@(stu.age),@(stu.height),@(stu.weight)]mutableCopy];
NSLog(@"%@",array);
第二种:plist文件保存
//获得路径并保存
NSString *sysString = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [sysString stringByAppendingPathComponent:@"text.plist"];
NSLog(@"%@",path);
NSDictionary *dic1 = @{@"1":@"yangang",@"2":@"dingwei",@"3":@"zengjianxiong"};
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"dsfsd",@"name",@"6",@"age",@"girl",@"sex",nil];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:dic1 forKey:@"one"];
[dic setObject:dic2 forKey:@"two"];
[dic writeToFile:path atomically:YES];
//读取数据
NSString *sysString = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [sysString stringByAppendingPathComponent:@"text.plist"];
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@",data);
第三种:单例保存
[[NSUserDefaults standardUserDefaults]setObject:@"123" forKey:@"name"];//保存
NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];//读取
下面介绍三种数据持久化的方法
第二种:归档
创建一个类 并且遵守<NSCoding>这个协议
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int age;
@property(nonatomic,assign)double height;
写三个要保存的属性
-(void)encodeWithCoder:(NSCoder *)aCoder 当将一个自定义对象保存到文件的时候就会调用该方法
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{ 当从文件中读取一个对象的时候就会调用该方法
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.height = [aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
Student *stu = [Student new]; 初始化并赋值
stu.name = @"haha";
stu.age = 223232;
stu.height = 34234.532;
stu.weight = 34300002;
NSString *sysPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];获取文件路径并将文件的对象保存到文件中
NSString *path = [sysPath stringByAppendingPathComponent:@"student.lh"];
NSLog(@"%@",path);
[NSKeyedArchiver archiveRootObject:stu toFile:path];
读取数据
NSString *sysPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [sysPath stringByAppendingPathComponent:@"student.lh"];
Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSMutableArray *array = [@[stu.name,@(stu.age),@(stu.height),@(stu.weight)]mutableCopy];
NSLog(@"%@",array);
第二种:plist文件保存
//获得路径并保存
NSString *sysString = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [sysString stringByAppendingPathComponent:@"text.plist"];
NSLog(@"%@",path);
NSDictionary *dic1 = @{@"1":@"yangang",@"2":@"dingwei",@"3":@"zengjianxiong"};
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"dsfsd",@"name",@"6",@"age",@"girl",@"sex",nil];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:dic1 forKey:@"one"];
[dic setObject:dic2 forKey:@"two"];
[dic writeToFile:path atomically:YES];
//读取数据
NSString *sysString = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [sysString stringByAppendingPathComponent:@"text.plist"];
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@",data);
第三种:单例保存
[[NSUserDefaults standardUserDefaults]setObject:@"123" forKey:@"name"];//保存
NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];//读取
相关文章推荐
- 在 Linux 上使用 tarball
- swift数据持久化--归档
- 福建省漳州市医院信息系统备份归档容灾系统采购项目
- 归档,SharePoint的好伙伴
- 问答:归档产品如何保障数据安全(上)
- 2012 ISTE国际教育行业技术大会
- 保险IT应用高峰论坛
- 参加微软全球合作伙伴大会
- MessageSolution参加第五届中国保险IT应用高峰论坛(保网)
- NSUserDefaults删除整个plist文件
- SharePoint 2010 文档管理(三)过期归档工具
- ORACLE 9I&10G置归档方法
- NSCoder,NSCoding,NSCopying之间的区别
- Linux下用Archivemount把tar归档文件映射为虚拟磁盘
- 复杂对象、自定义对象写入沙盒
- iOS 归档 解档使用总结
- LogBack 归档日志和 使用
- iOS归档与反归档
- Obect-C:Archiving归档小结
- objective C归档