数据持久化

来源:互联网 发布:人脸识别网络考勤系统 编辑:程序博客网 时间:2024/06/16 07:34

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);}





0 0