iOS - UI-归档解归档

//

//  AppDelegate.m

//  UI-归档

//

//  Created by jzq_mac on 15/7/17.

//  Copyright (c) 2015 jzq_mac. All rights reserved.

//


#import "AppDelegate.h"

#import "UserModle.h"

@interface AppDelegate ()


@end


@implementation AppDelegate

/*

 沙盒:(sandbox)机制:是一种安全体系,ios应用程序值只能对自己创建的应用程序进行读取文件,这个独立,封闭,安全的空间,就是沙盒,它里面一般存放着程序需要的文件,数据持久化的文件(Plist sqlite)音视频,图片以及其他资源文件

 

 每一个应用程序只要一个沙盒

 沙盒里面有3个文件

 1.documents 目录下的内容会被同步到另一台设备,可以放媒体资源,文本资源

 2.library 可以把缓存的内容放到liability/cashes

 3.temp 创建临时文件的目录,设备重启时自动清除

 

*/


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

#pragma ----------------------沙盒---------------------------------

获得根目录的方式

//    NSString *homePath = NSHomeDirectory();

//    NSLog(@"%@",homePath);

  获得documents目录的方式

//    NSArray *pahs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    

//    NSString *documentPath = [pahs firstObject];

//    NSLog(@"%@",documentPath);

  library目录

//    

//    NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

//    NSString *libraryPath = [path2 lastObject];

//    NSLog(@"%@",libraryPath);

//    

  前往libility/cashes目录

临时文件目录temp

//    NSString *tempPath = NSTemporaryDirectory();

//    NSLog(@"%@",tempPath);

    

    

#pragma ----------------------归档1---------------------------------

//  归档(又叫序列化coding)NSKeyedArchiver 解归档:NSKeyedUnarchiver

//  归档之后的文件会被加密;可以归档的文件,数据:任何数据(直接归档一个类实例化的对象 数组 字典 字符串 文本 图片)

//  归档步骤 1.归档的路径 需要归档的数据 2.归档,解归档

    

//    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    NSString *documentPath = [path lastObject];

//    NSString *path1 = [documentPath stringByAppendingPathComponent:@"userList.archiver"];//拼接归档文件路径

//    NSArray *users = @[@"张三",@"李四",@"王二"];

//    BOOL succese = [NSKeyedArchiver archiveRootObject:users toFile:path1];

//    if (succese) {

//        [self showAlertViewWithMessage: @"归档成功"];

//    }

//    NSLog(@"%@",path1);

//  解归档:1.解归档文件路径 2.解归档

//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    NSString *path = [[paths lastObject]stringByAppendingPathComponent:@"userList.archiver"];

//    NSArray *list = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

//    for (NSString *name in list) {

//        NSLog(@"%@",name);

//    }

//    

//    NSMutableArray *arr = [list mutableCopy];//添加数组元素

//    [arr addObject:@"麻子"];

//    for (NSString *name in arr) {

//        NSLog(@"%@",name);

//    }


#pragma ----------------------第二种归档方式---------------------------------

 

//NSData 二进制数据的类

// 1.归档文件路径 准备需要归档的数据 2.写一个可变的NSMutableData 通过归档的类让Data准备写入到文件 3.开始归档(编码)4.结束编码,归档 data数据写入到文件

    

 // 1.归档文件路径 准备需要归档的数据

//    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    NSString *documentPath = [path lastObject];

//    NSString *path1 = [documentPath stringByAppendingPathComponent:@"userList.archiver"];//拼接归档文件路径

//

//    NSArray *list = @[@"13",@"14",@"520"];

//    NSDictionary *dic = @{@"key":@"value"};

//    

  2.写一个可变的NSMutableData 通过归档的类让Data准备写入到文件

//    

//    NSMutableData *data = [[NSMutableData alloc]init];

//    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    3.开始归档 编码(编码 和解码的时候key一定要相同)

//    [archiver encodeObject:list forKey:@"list"];

//    [archiver encodeObject:dic forKey:@"dic"];

  4.结束编码,归档 data数据写入到文件

//    [archiver finishEncoding];

    atomically 如果设置成YES的时候在写入数据时 如果出现意外情况(用户突然强制退出了应用程序,断电等等)会把写入到临时文件的内容清理掉,不再写入到目标文件;atomically 如果设置成NO的时候,不管数据是否是完整都会直接写入到目标文件

//    BOOL successe =  [data writeToFile:path1 atomically:YES];

//    if (successe) {

//        [self showAlertViewWithMessage:@"归档成功"];

//    }

    

//  解归档:1.文件路径 2.读取文件里面data数据 3.通过解归档对象读取data数据准备解归档 4.解归档数据

//   1.文件路径 

//    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    NSString *documentPath = [path lastObject];

//    NSString *path1 = [documentPath stringByAppendingPathComponent:@"userList.archiver"];//拼接归档文件路径

2.读取文件里面data数据

//    

//    NSData *data = [NSData dataWithContentsOfFile:path1];

3.通过解归档对象读取data数据准备解归档

//    NSKeyedUnarchiver *unAchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

  4.解归档数据(解码)

//    

//    NSArray *arr = [unAchiver decodeObjectForKey:@"list"];

//    NSDictionary *dic = [unAchiver decodeObjectForKey:@"dic"];

//    NSLog(@"%@\n%@",arr,dic);

//    

 #pragma ---------------------想归档 自定义的类的对象-------------------------------

/*

 想归档 自定义的类的对象

 1.遵守NSCoding协议

 2.实现归档协议里面的方法

 3.逐一对自定义的属性 进行了编码和解码 者时候这个对象就具备了归档,解归档的功能

 4.开始归档 解归档

*/

// 归档

//1.准备要归档文件路径和要归档的数据

//2.进行归档

    

1.准备要归档文件路径和要归档的数据

//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    NSString *path = [[paths lastObject]stringByAppendingPathComponent:@"userModel.archiver"];

//    UserModle *model = [[UserModle alloc]init];

//    model.name = @"葫芦娃";

//    model.age = 18;

//    

//     BOOL success = [NSKeyedArchiver archiveRootObject:model toFile:path];

//    if (success) {

//        [self showAlertViewWithMessage:@"归档成功"];

//    }else

//    {

//        [self showAlertViewWithMessage:@"归档不成功"];

//    }

// 2.解码

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path = [[paths lastObject]stringByAppendingPathComponent: @"userModel.archiver"];

    UserModle *model = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    NSLog(@"%@ %ld",model.name,model.age);

    return YES;

}



- (void)showAlertViewWithMessage:(NSString *)message

{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"恭喜" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];

    

}

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值