美文网首页
归档解当 iOS

归档解当 iOS

作者: 三思的简书 | 来源:发表于2017-02-07 11:23 被阅读0次
    //文件路径
    -(NSString *)cachesTestPathWithFileName:(NSString *)fileName{
        NSString * homePath=NSHomeDirectory();
        NSString * libraryPath=[homePath stringByAppendingPathComponent:@"Library"];
        NSString * cachesPath=[libraryPath stringByAppendingPathComponent:@"Caches"];
        return [cachesPath stringByAppendingPathComponent:fileName];
    }
    
    // 1. 单个对象的归档解档
        NSArray * arr=@[@"hello"];
        //获取归档路径
        NSString * keyPath=[self cachesTestPathWithFileName:@"arr.key"];
        //执行归档操作
        BOOL rel=[NSKeyedArchiver archiveRootObject:arr toFile:keyPath];
        if (rel) {
            NSLog(@"完成");
        }
        //执行解档操作
        NSArray * arrOut=[NSKeyedUnarchiver unarchiveObjectWithFile:keyPath];
    
    // 2.多个对象归档到一个文件中
        //用于存储归档进的对象数据。
        NSMutableData * data=[[NSMutableData alloc]init];
        //实例化归档对象
        NSKeyedArchiver * keyedArchiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
        //开始归档多个对象
        [keyedArchiver encodeObject:@{@"a":@"b"} forKey:@"dic"];
        [keyedArchiver encodeDouble:1.414 forKey:@"gen2"];
        [keyedArchiver encodeBool:YES forKey:@"bool"];
        [keyedArchiver finishEncoding];
        //把存储归档数据的data保存到本地
        NSString * theDataPath=[self cachesTestPathWithFileName:@"save"];
        [data writeToFile:theDataPath atomically:NO];
        
        //多对象解档
        NSData * outData=[NSData dataWithContentsOfFile:theDataPath];
        //创建解档对象
        NSKeyedUnarchiver * unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:outData];
        NSDictionary * outDic=[unArchiver decodeObjectForKey:@"dic"];
        NSLog(@"%@",outDic);
        NSLog(@"%f",[unArchiver decodeDoubleForKey:@"gen2"]);
    
    在 .h 文件中
    //自定义类,想要实现归档接档,要遵守NSCoding协议
    @interface Student : NSObject
    <NSCoding>
    
    @property (nonatomic,copy) NSString * name;
    @property (nonatomic) float score;
    
    @end
    
    在 .m 文件中
    //实现归档协议方法
    -(void)encodeWithCoder:(NSCoder *)aCoder{
        //对属性进行归档操作
        [aCoder encodeObject:self.name forKey:@"name"];
        [aCoder encodeFloat:self.score forKey:@"score"];
    }
    
    //实现解档协议方法
    -(instancetype)initWithCoder:(NSCoder *)aDecoder{
        if (self=[super init]) {
            //把自身属性重新赋值。
            self.name=[aDecoder decodeObjectForKey:@"name"];
            self.score=[aDecoder decodeFloatForKey:@"score"];
        }
        return self;
    }
    
    @end
    
    //  3. 实现自定义类的归档和解档
        Student * stu=[[Student alloc]init];
        stu.name=@"小明";
        stu.score=80;
        //归档
        NSString * stuPath=[self cachesTestPathWithFileName:@"stu.key"];
        [NSKeyedArchiver archiveRootObject:stu toFile:stuPath];
        //解档
        Student * outStu=[NSKeyedUnarchiver unarchiveObjectWithFile:stuPath];
        NSLog(@"%@ %f",outStu.name,outStu.score);
    

    相关文章

      网友评论

          本文标题:归档解当 iOS

          本文链接:https://www.haomeiwen.com/subject/xkbpittx.html