您的位置:首页 > 产品设计 > UI/UE

UISenior之数据的本地化持久化

2016-05-07 09:36 218 查看
数据的本地化主要分为两个方面:1.简单数据的本地持久化(NSString、NSArray、NSDictionary、NSData)2.复杂数据的本地持久化(本文以Person类为例)

简单对象的本地化:

简单对象的本地化基本可以分为四步:

第一步:找到Documents的文件夹的路径。

第二步:我们要知道存储什么,所以要创建存储的对象

第三步:需要知道字符串最终的存储的地方,所以需要创建一个路径去存储字符串

第四步:准备工作完成,将字符串写入文件

下面具体实现上述四步:

第一步
//    第一步:需要知道这个对象存在哪里,所以需要一个文件夹的路径
//    找到Documents文件夹路径
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


第二步

//    第二步:我们知道要存储什么?所以需要创建什么
//    创建要存储的内容:字符串
NSString *str = @"AJAR";
第三步

//    第三步:需要知道字符串最终存储的地方,所以需要创建一个路径去存储字符串
NSString *strPath = [documents stringByAppendingPathComponent:@"zhy.txt"];


第四步

<span style="color:#000000;">//    第四步:准备工作完成,将字符串写入文件
//    第一个参数:写入的文件的一个路径
//    第二个参数:在断电的情况下,会不会自动保存
//    第三个参数:编码格式
//    第四个参数:错误信息
[str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"strPath = %@", strPath);</span>


将NSString文件夹存储的内容取出

//    将字符串取出:使用stringWithContentsOfFile这个方法将其取出
//    第一个参数:字符串存储的路径
//    第二个参数:编码格式
//    第三个参数:错误信息
NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"newStr = %@", newStr);


复杂对象的本地化:

复杂对象的本地化最重要的两步就是归档和解档

本文以Person对象为例,首先创建Person类继承于NSObject

Person.h文件

///姓名
@property(nonatomic, copy)NSString *name;

///性别
@property(nonatomic, copy)NSString *gender;

///年龄
@property(nonatomic, assign)NSInteger age;


Person.m文件中 需要重写归档和解档的方法

//归档
//将所有的属性进行归档
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.gender forKey:@"gender"];
[aCoder encodeInteger:self.age forKey:@"age"];
}

//解档(反归档)
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}


在ViewController里对Person对象进行操作

找到Documents文件夹的目录

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


创建Person对象

<span style="font-size:14px;">    Person *per = [[Person alloc] init];
per.name = @"MBboy";
per.gender = @"boy";
per.age = 18;</span>


把这个复杂对象归档

//   创建NSMutableData,用于创建初始化归档工具的
NSMutableData *data = [NSMutableData data];
NSLog(@"======data = %@====", data);
//   创建归档工具
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//   对要归档的person对象进行归档
[archiver encodeObject:per forKey:@"person"];
//   结束归档
[archiver finishEncoding];
NSLog(@"======data = %@====", data);


将归档的内容NSMutableData存储在本地

NSString *personPath = [documentPath stringByAppendingPathComponent:@"person.plist"];


写入

[data writeToFile:personPath atomically:YES];
NSLog(@"personPath = %@", personPath);


解档

将要解档的数据找出来

NSData *resultData = [NSData dataWithContentsOfFile:personPath];
NSLog(@"resultData = %@", resultData);
创建解档工具

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:resultData];
对person对象进行解档[使用对象接收]

Person *newPer = [unarchiver decodeObjectForKey:@"person"];
结束解档

[unarchiver finishDecoding];
NSLog(@"name = %@ gender = %@ age = %ld", newPer.name, newPer.gender, newPer.age);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: