zoukankan      html  css  js  c++  java
  • 归档反归档

       
    //归档 反归档(麻烦的方法)
        /*
        //归档
        //创建Person实例对象
        Person *person1=[[Person alloc]init];
        person1.name=@"bbb";
        person1.age=@"39";
        
        Person *person2=[[Person alloc]init];
        person2.name=@"aaa";
        person2.age=@"18";
        
        
        //归档使用的NSData
        NSMutableData *person1Data=[NSMutableData data];
        
        //创建归档工具
        NSKeyedArchiver *achiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:person1Data];
        
        //进行归档(需要宏定义)
        [achiver encodeObject:person1 forKey:kPerson1];
        [achiver encodeObject:person2 forKey:kPerson2];
        
        
        //完成转换
        [achiver finishEncoding];
        
        //获取Documents路径
        NSString *documentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        //拼接文件路径
        NSString *personPath=[documentsPath stringByAppendingString:@"/刘杰.xxoo"];
        
        //写入
        [person1Data writeToFile:personPath atomically:YES];
        NSLog(@"documents:%@",documentsPath);
        
        
        
        
        
        
        //反归档
        //通过文件路径获取data数据
        NSData *unData=[NSData dataWithContentsOfFile:personPath];
        
        //创建反归档工具
        NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:unData];
        
        //进行反归档
        Person *p1=[unArchiver decodeObjectForKey:kPerson1];
        Person *p2=[unArchiver decodeObjectForKey:kPerson2];
        
        //结束反归档
        [unArchiver finishDecoding];
        NSLog(@"name:%@",p1.name);
        NSLog(@"age:%@",p1.age);
        NSLog(@"name:%@",p2.name);
        NSLog(@"age:%@",p2.age);
        */

        
        
    //归档 反归档(简单方法)
        
        //获取Documents路径
        NSString *docPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
        //拼接文件路径
        NSString *filePath=[docPath stringByAppendingString:@"/personArray.plist"];
        
        /*
         //只能归档一个对象
        Person *pn1=[[Person alloc]init];
        pn1.name=@"王小二";
        pn1.age=@"20";
        
        //归档
        [NSKeyedArchiver archiveRootObject:pn1 toFile:filePath];
        
        //反归档
        Person *p1=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"name:%@",p1.name);
        NSLog(@"age:%@",pn1.age);
        */
        
        
        Person *pn1=[[Person alloc]init];
        pn1.name=@"Tom";
        pn1.age=@"12";
        
        Person *pn2=[[Person alloc]init];
        pn2.name=@"Kim";
        pn2.age=@"25";
        
        
        NSArray *array=@[pn1,pn2];//把两个数据放到数组里面
        

        
        //反归档
        NSArray *a=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        
        NSLog(@"name:%@",[a[0] name ]);
        NSLog(@"age:%@",[a[0] age]);
        
        NSLog(@"name:%@",[a[1] name]);
        NSLog(@"age:%@",[a[1] age]);
        
        
        
        
        
        
        

  • 相关阅读:
    创建vlan 和 节点vlan 连通性排查
    FRRouting SR-MPLS
    mpls over gre
    linux mpls
    交换机vlan
    neutron subnet + router
    neutron 层次绑定 +binding_levels
    frrouting命令补全 + 启动失败排查
    Paper Pal:一个中英文论文及其代码大数据搜索平台
    游戏服务器设计 Unity3d + photon + grpc + nodejs + postgis/postgresql
  • 原文地址:https://www.cnblogs.com/-ios/p/4672928.html
Copyright © 2011-2022 走看看