iOS 本地数据持久化
来源:互联网 发布:云计算现状 编辑:程序博客网 时间:2024/04/30 08:08
本地数据持久化
1. 使用本地文件读写方式的本地数据持久化
对于通过本地文件读写方式的本地数据持久化来说, 只支持 字符串, 数组, 字典, 二进制数据(NSData)
- 字符串的读写
/字符串的写入过程(实现文件内容拼接)/
//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; - 数组的读写
//将数组写入文件
[stringArray writeToFile:filePath atomically:YES];//stringPath 是一个数组
//将数组从文件中读取出来
NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePath];
self.firstTF.text = [dataArray firstObject];
self.secondTF.text = [dataArray lastObject]; - 字典的读写
//将字典写入文件
[dic writeToFile:filePath atomically:YES];
//将字典从文件中读取
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.firstTF.text = dic[@"tf1"];
self.secondTF.text = dic[@"tf2"]; - 二进制数据的读写
//将二进制数据写入文件
[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
- iOS 本地数据持久化
- ios实现 本地存储 数据持久化
- IOS 本地储存(数据持久化)
- iOS本地数据库(数据持久化)
- ios本地数据持久化方法
- ios数据本地持久化 之 使用文件持久化
- iOS 本地数据持久化 中UserDefault的使用
- iOS开发中的本地数据存储(持久化)
- iOS项目拆分:数据本地持久化(1)
- iOS项目拆分:数据本地持久化(2)
- iOS项目拆分:数据本地持久化(3)
- iOS项目拆分:数据本地持久化(4)
- iOS 本地持久化 "归档"
- iOS本地持久化问题
- ios本地持久化存储
- 本地存储(数据持久化)
- IOS 数据持久化
- iOS 数据持久化
- 在北京黑马学习Java01
- SSH面试常考题(6)
- 通过Android trace文件分析死锁ANR
- elasticsearch 索引 Warmer 管理
- 关于C语言0x01
- iOS 本地数据持久化
- Arrays.ArrayList 固定长度的List集合
- Codeforces Round #299
- K-Means++的代码
- linux时间函数总结
- Spring constructor-arg、c、p
- ValueError: urls must start with a leading slash
- ionic初步测试-listView效果
- 删除SQL Server 2008遇到的问题