数据持久化
发布日期:2022-02-08 18:03:26
浏览次数:30
分类:技术文章
本文共 5704 字,大约阅读时间需要 19 分钟。
1. 归档和反归档:
归档的时候一定要使归档的对象遵循协议:NSCoding
创建一个Person类,有俩个属性:name age 在实现文件中写:
// 对属性做归档处理-(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"];}// 反归档处理:注意属性的类型 做对应的操作(归档类似)-(instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"]; } return self;}
- (void)viewDidLoad { [super viewDidLoad]; #pragma mark ***** 归档 反归档 ***** #pragma mark 1.归档 // 1. 创建对象 Person *person1 = [[Person alloc] init]; person1.name = @"haha-sha"; person1.age = 17; // 2.创建NSKeyedArchiver对象 NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];//NSMutableData // 3.NSKeyedArchiver对象调用归档方法 [archiver encodeObject:person1 forKey:@"p1"];// key 设为了p1 后面反归档的时候也得是p1 // 4. 结束归档 [archiver finishEncoding]; // 5. 获取存数据的路径 NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; // 创建存数据的文件 NSString *newPath = [cachesPath stringByAppendingPathComponent:@"person"]; NSLog(@"%@", cachesPath); // 写入数据 [data writeToFile:newPath atomically:YES]; #pragma mark 2. 反归档 // 1. 获取数据路径 NSData *data1 = [NSData dataWithContentsOfFile:newPath]; // 2. 对获取的数据进行反归档 NSData NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data1]; Person *p = [unarchiver decodeObjectForKey:@"p1"]; // 3. 结束反归档 [unarchiver finishDecoding]; NSLog(@"%@ %ld", p.name, p.age);}
2. 沙盒机制:
1. 获取沙盒对应文件的路径,之后拼接创建文件
#pragma mark 获取沙盒中文件路径 // 获取沙盒的路径 // ios8之后每次运行路径都会改变 NSString *sandBoxPath = NSHomeDirectory(); NSLog(@"%@", sandBoxPath); // document路径:存放程序运行时生成的文件,这个文件不要存放较大的东西(例如:音频 视频),因为这里的东西会被上传 NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@", documentPath); // 获取library中的caches路径:一般用于文件的下载,存储 (不会被上传) NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@", cachesPath); // 获取tmp路径 和沙盒相似 调用一个方法 // 存放临时文件,程序结束后,应该清空 NSString *tmpPath = NSTemporaryDirectory(); NSLog(@"%@", tmpPath); NSLog(@"%@", [NSBundle mainBundle]);2. 简单对象 写入:字典 数组 字符串 图片
#pragma mark 简单对象写入文件: (简单对象:字符串 数组 字典等等) // 1. 写入字符串 NSString *str = @"呵呵哒"; // 获取document路径 NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 在document的路径上拼接一个txt文件 NSString *filePath1 = [documentPath stringByAppendingPathComponent:@"haha.txt"]; // 将字符串内容写到之前的txt文件里 [str writeToFile:filePath1 atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@", documentPath); // 读取数据: NSString *str2 = [NSString stringWithContentsOfFile:filePath1 encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@", str2); // 2. 写入数组:NSArray NSArray *array = @[@"二傻子", @"翠花", @"王尼玛", @"张全蛋", @"赵铁柱", @"李小花"]; NSString *tmpPath1 = NSTemporaryDirectory(); NSString *filePath2 = [tmpPath1 stringByAppendingPathComponent:@"什么鬼.plist"]; [array writeToFile:filePath2 atomically:YES]; NSArray *a = [NSArray arrayWithContentsOfFile:filePath2]; NSLog(@"%@", a); // 3. 写入字典 NSDictionary *dic1 = @{@"name":@"张无忌", @"skill":@"乾坤大挪移"}; NSDictionary *dic2 = @{@"name":@"郭靖", @"skill":@"降龙十八掌"}; NSDictionary *dic3 = @{@"name":@"段誉", @"skill":@"凌波微步"}; NSMutableArray *array = @[dic1, dic2, dic3]; NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@", cachesPath); NSString *filePath3 = [cachesPath stringByAppendingPathComponent:@"字典.plist"]; [array writeToFile:filePath3 atomically:YES]; [dic3 writeToFile:filePath3 atomically:YES];
NSString *cachesPath2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@", cachesPath2); NSString *filePath4 = [cachesPath2 stringByAppendingPathComponent:@"haha.png"]; // 初始化进行赋值:// NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"h14" ofType:@"jpg"]]; // 不初始化也可以 直接赋值: NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"h14" ofType:@"jpg"]]; [data writeToFile:filePath4 atomically:YES];
3. NSFileManager:
#pragma mark NSFileManager// 1. 操作文件夹: // 1> 获取沙盒路径 NSString *document3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@", document3); // 2> 创建NSFileManager NSFileManager *manager = [NSFileManager defaultManager]; // 3> 拼接 NSString *newFile =[document3 stringByAppendingPathComponent:@"尼玛"]; // 4> 在指定的路径下创建文件夹 [manager createDirectoryAtPath:newFile withIntermediateDirectories:YES attributes:nil error:nil]; // 2. 移动文件夹: NSString *movePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"尼玛"]; NSLog(@"%@", movePath); [manager moveItemAtPath:newFile toPath:movePath error:nil]; // 3. 判断是否存在文件夹或文件 BOOL r = [manager fileExistsAtPath:movePath]; r = [manager fileExistsAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"fshfjs"]]; NSLog(@"%d", r); // 4. 删除文件夹:// [manager removeItemAtPath:movePath error:nil];// NSLog(@"%@", movePath); // 5. 复制文件夹: [manager copyItemAtPath:movePath toPath:newFile error:nil]; NSLog(@"%@", movePath);}
转载地址:https://blog.csdn.net/Evelynzn/article/details/49665517 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年03月08日 08时53分47秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
后缀树建立 java_实用算法实现-第 8 篇后缀树和后缀数组 [2 最长公共子串]
2019-04-21
java网络编程期末试题_java网络编程面试题【其中一小部分】
2019-04-21
java基金管理系统_【Java GUI】Java GUI基金会
2019-04-21
docker 部署java_docker 部署java环境以及常用应用(持续更新)
2019-04-21
jpmml导出java文件_JPMML解析PMML模型并导入数据进行分析生成结果
2019-04-21
java如何做表单校验_微信小程序实现表单校验功能
2019-04-21
php类里面调用函数吗,从PHP类中的另一个函数调用一个函数
2019-04-21
php7 ast,PHP7 的抽象语法树(AST)带来的变化,_PHP_ 少侠科技
2019-04-21
windows 2003 php5.6,GitHub - d93921012/php-5.6-xp-2003: Run PHP 5.6+ on Windows XP and 2003
2019-04-21
php 微信支付宝提现,微信支付对接提现功能(php)
2019-04-21
php 薛强,PHP框架Yii系列教程(一):入门实例
2019-04-21
oracle的java包,oracle中程序包?
2019-04-21
php先学html,PHP入门学习–HTML01
2019-04-21
mysql中字符转数字,MYSQL字符数字转换为数字
2019-04-21