iOS 本地数据持久化

来源:互联网 发布:云计算现状 编辑:程序博客网 时间:2024/04/30 08:08

本地数据持久化

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
原创粉丝点击