iOS 本地数据持久化

本地数据持久化

1. 使用本地文件读写方式的本地数据持久化

对于通过本地文件读写方式的本地数据持久化来说, 只支持 字符串, 数组, 字典, 二进制数据(NSData)

  1. 字符串的读写
     
    /字符串的写入过程(实现文件内容拼接)/
    //1. 获取源文件的路径
    //1). 获取 Documents (存放永久存储的数据)文件夹路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //2). 拼接上文件路径
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"sara.txt"];
    //2. 获取源文件的内容
    NSString *oldStr = [NSString stringWithContentOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    //3. 将原有数据与新数据拼接在一起
    NSString *newStr = [oldStr stringByAppendingFormat:@"%@", self.firstTF.text];
    if (!newStr) {
    newStr = self.firstTF.text;
    }
    //4. 将新的字符串写入文件
    [newStr writeToFile:filePath automically:YES encoding:NSUTF8StringEncoding error:nil];//atomically 保证写入安全
    /字符串的读取过程/
    NSString *text = [NSString stringWithContentsO分File:filePath encoding:NSUTF8StringEncoding error:nil];
    //让第二个输入框显示
    self.secondTF.text = text;
  2. 数组的读写
    //将数组写入文件 
    [stringArray writeToFile:filePath atomically:YES];//stringPath 是一个数组
    //将数组从文件中读取出来
    NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePath];
    self.firstTF.text = [dataArray firstObject];
    self.secondTF.text = [dataArray lastObject];
  3. 字典的读写
    //将字典写入文件 
    [dic writeToFile:filePath atomically:YES];
    //将字典从文件中读取
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    self.firstTF.text = dic[@"tf1"];
    self.secondTF.text = dic[@"tf2"];
  4. 二进制数据的读写
    //将二进制数据写入文件 
    [data writeToFile:filePath atomically:YES];
    //将二进制数据从文件中读取出来
    NSData *data = [NSData dataWithContentOfFile:filePath];

2. 使用归档与反归档方式的本地数据持久化

//Contact类服从NSCoding 协议
//当对一个对象进行归档时, 自动调用该方法, 为该对象的实例变量进行归档操作
-(void)encodeWithCoder:(NSCoder *)aCoder {
    //内部要对每一个实例变量进行归档
    [aCoder encodeObject:_name forkey:@"name"];
    [aCoder encodeObject:_gender forKey:@"gender"];
    [aCoder encodeObject:@(_age) forKey:@"age"];
    [aCoder encodeObject:_phone forKey:@"phone"];
}
//当对一个对象进行反归档时触发, 自动调用该方法, 为该对象的实例变量进行反归档操作
-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.gender = [aDecoder decodeObjectForKey:@"gender"];
        self.age = [[aDecoder decodeObjectForKey:@"age"] integerValue];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
    }
    return self;
}
//一. 归档
//将自定义的联系人对象contact进行本地数据持久化
//Contact *contact = [[Contact alloc] initWithName:@"Sara Burton" Gender:@"女" Age:18 phone:@"150xxxxxxxx"];
//1. 创建归档工具对象
NSMutableData *mData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
//2. 开始归档
[archiver encodeObject:contact forKey:@"contact"];
//3. 结束归档
[archiverfinishEncoding];// 一个结束归档就可以了
[contact release];
[archiver release];
//4. data 写入文件
[mData writeToFile:filePath atomically:YES];
//二. 反归档
//1. 从本地读取数据
NSData *data = [NSData dataWithContentsOfFile: filePath];
//2. 创建反归档工具
NSkeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//3. 开始反归档
Contact *contact = [unarchiver decodeObjectForKey:@"key"];
//4. 结束反归档
[unarchiver finishDecoding];
//5. 释放
[unarchiver release];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值