• 归档反归档


       
    //归档 反归档(麻烦的方法)
        /*
        //归档
        //创建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]);
        
        
        
        
        
        
        

  • 相关阅读:
    UVA
    [CQOI2018] 社交网络
    UVA
    51nod 1314 定位系统
    51nod 1211 数独
    51nod 1392 装盒子
    51nod1253 Kundu and Tree
    51nod1313 完美串
    51nod1039 x^3 mod p
    51nod1369 无穷印章
  • 原文地址:https://www.cnblogs.com/-ios/p/4672928.html
Copyright © 2020-2023  润新知