数据持久化
来源:互联网 发布:双色球矩阵选号器 编辑:程序博客网 时间:2024/05/18 17:54
ViewController.m
#import "ViewController.h"#import "Person.h"@interface ViewController ()@property (nonatomic,strong)UIImageView * imv;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 沙盒就是一个文件夹(说明一定有路径)// 获取沙盒路径 /*// 获取用户名 NSString * s = NSUserName();// 获取主路径 NSString * rootPath = NSHomeDirectoryForUser(s); // 沙盒路径一直在变,(iOS8之前都是固定的) NSLog(@"%@",rootPath); // other NSString * rootPath1 = NSHomeDirectory(); NSLog(@"%@",rootPath1); // Documents会放变动很小的东西,不会轻易删除添加,所有会放数据库类似的东西,不要放缓存文件(电影之类的),不要放很大的东西,会连接到iCloud// Library中的Cashes会放缓存的东西(退出后还有)// tmp会放临时的东西,用完删,比如电影中的缓存,(退出后没了)// 这三个文件夹都是运行后才有东西,初始不带东西 */ // 获取三个路径 /*// 获取Documents路径// 作用:数据运行后产生,主要存储数据库等不长改变的数据文件.存在这里的文件会被备份.(下载的文件不能放在这)// 返回值是数组 NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",documentsPath); // 获取Caches路径// 作用:能放缓存文件,eg:音频,视频,图片:(不会被自动备份) NSString * cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",cachesPath); // 获取tmp路径// 作用:存放临时文件,程序下次启动不需要,退出清空 NSString* tmpPath = NSTemporaryDirectory(); NSLog(@"%@",tmpPath); */ // 简单文件的写入 /* // 字符串// 准备路径 NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",documentsPath); // NSString,NSArray,NSDictionary,NSData// 文件路径 NSString * str1 = @"Hello,World"; NSString * filePath = [documentsPath stringByAppendingString:@"/hello.txt"];//// 写入// [str1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];// //// 读出// NSString * s1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];// // NSLog(@"%@",s1); // 数组 // 后缀随便写 NSString * filePath2 = [documentsPath stringByAppendingPathComponent:@"array.map3"]; NSArray * array1 = @[@"1",@"2",@"3"]; //// 写入//// [array1 writeToFile:filePath2 atomically:YES];// //// 读// NSArray *array2 = [NSArray arrayWithContentsOfFile:filePath2];// NSLog(@"%@",array2); // 字典 NSString * filePath3 = [documentsPath stringByAppendingPathComponent:@"dictionary.mp3"]; NSDictionary * dict1 = @{@"a":@"1",@"b":@"2"}; // 写入// [dict1 writeToFile:filePath3 atomically:YES]; // 读出 NSDictionary * dict2 = [NSDictionary dictionaryWithContentsOfFile:filePath3]; NSLog(@"%@",dict2); */ // 复杂对象的写入//// 这种归档可以一次归档很多 /*// Person * p1 = [[Person alloc]init];// p1.name = @"贝爷";// p1.age = 10;////// 准备一个可变Data// NSMutableData * data = [NSMutableData data];// //// 创建归档工具// NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];// //// 开始归档// [archiver encodeObject:p1 forKey:@"p1"];// //// 完成归档// [archiver finishEncoding]; // 存档后,要放入沙盒中 // 准备路径 NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",documentsPath); // 准备文件路径 NSString * filePath4 = [documentsPath stringByAppendingPathComponent:@"贝爷.m"]; //// data写入// [data writeToFile:filePath4 atomically:YES]; // 读出 NSData * data1 = [NSData dataWithContentsOfFile:filePath4]; // 反归档 NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1]; // 转换成对象 Person * p2 = [unarchiver decodeObjectForKey:@"p1"]; // 反归档完成 [unarchiver finishDecoding]; NSLog(@"%@ == %ld",p2.name,p2.age); */ // 这种方便,但是一次只能归档一个(不常用) /* // 准备路径 NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",documentsPath); // 拼接路径 NSString * filePath5 = [documentsPath stringByAppendingPathComponent:@"person"]; Person * p3 = [[Person alloc]init]; p3.name = @"六娃"; p3.age = 20; // 存// [NSKeyedArchiver archiveRootObject:p3 toFile:filePath5];// 取 Person *p4 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath5]; NSLog(@"%@ = %ld",p4.name,p4.age); */ // NSUserDefaults// 用来存储不是很大的东西 /* NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; // 存// [ud setObject:@"yan3" forKey:@"lanou"];//// 同步数据// [ud synchronize]; // 取// NSLog(@"%@",[ud objectForKey:@"lanou"]); */ // 导航页测试 /* NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; NSString * s = [ud objectForKey:@"first"]; if (s == nil) { self.imv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hehe.png"]]; self.imv.frame = self.view.bounds; [self.view addSubview:_imv]; [ud setObject:@"NO" forKey:@"first"]; } */ // NSFileManager // 路径 NSString * cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",cachesPath); // 创建文件管理对象 NSFileManager * fm = [NSFileManager defaultManager]; // 创建文件夹// [fm createDirectoryAtPath:[cachesPath stringByAppendingPathComponent:@"test"] withIntermediateDirectories:NO attributes:nil error:nil]; // 更改文件名// [fm moveItemAtPath:[cachesPath stringByAppendingPathComponent:@"test"] toPath:[cachesPath stringByAppendingPathComponent:@"Demo"] error:nil]; // 移动文件位置// [fm moveItemAtPath:[cachesPath stringByAppendingPathComponent:@"00/test"] toPath:[cachesPath stringByAppendingPathComponent:@"Demo/test"] error:nil]; // 删除文件// [fm removeItemAtPath:[cachesPath stringByAppendingPathComponent:@"Demo"] error:nil]; // 判断一个文件是否存在 BOOL i = [fm fileExistsAtPath:@"asdad"]; NSLog(@"%d",i); }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
Person.h
#import <Foundation/Foundation.h>//一个类是否可以被归档反归档,要看是否遵循NSCoding协议@interface Person : NSObject<NSCoding>@property (nonatomic,copy)NSString * name;@property (nonatomic,assign)NSInteger age;@end
Person.m
#import "Person.h"@implementation Person//编码- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"p_name"]; [aCoder encodeInteger:self.age forKey:@"p_age"];}//反编码- (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { self.name = [aDecoder decodeObjectForKey:@"p_name"]; self.age = [aDecoder decodeIntegerForKey:@"p_age"]; } return self;}@end
0 0
- 什么是数据持久化?
- 数据持久化
- 数据持久化
- 什么是数据持久化
- 数据持久化
- 数据持久化
- redis数据持久化
- Android 数据持久化
- 数据持久化
- 数据持久化
- JForum数据持久化
- IOS 数据持久化
- 浅谈数据持久化
- 浅谈数据持久化
- Android 数据持久化
- iOS 数据持久化
- 数据持久化
- ios数据持久化
- Linux信号编程实践(二) 信号发送函数和可重入函数
- ruby 学习笔记 2 -变量
- 第三周项目(4):顺序表的基本运算的总和
- java 给定一个非负整数num,反复添加所有的数字,直到结果只有一个数字
- log4j配置祥解
- 数据持久化
- iOS单个方向的圆角处理
- 支持任意手势动作的图片
- xcode7.0 下COCOS2DX编译MAC下出错
- 在不更改访问时间和修改时间的前提下截断文件
- Bash编程七大展开之参数展开(Parameter Expansion)
- yum 改数据源
- windows下安装codecombt简易中文教程
- Android权限之sharedUserId和签名