随波逐流之IOS 复杂数据的归档和反归档 详解

objc] view plaincopy

    Person.h  

[objc] view plaincopy

    //  
    //  Person.h  
    //  MySandBox  
    //  
    //  Created by long on 15/9/15.  
    //  Copyright (c) 2015年 WLong. All rights reserved.  
    //  
      
    #import <Foundation/Foundation.h>  
    @interface Person : NSObject<NSCoding>  
    @property (nonatomic,retain) NSString *name;  
    @property (nonatomic,assign) NSInteger age;  
    @property (nonatomic,retain) NSData *data;  
    @end  


[objc] view plaincopy

    //  
    //  Person.m  
    //  MySandBox  
    //  
    //  Created by long on 15/9/15.  
    //  Copyright (c) 2015年 WLong. All rights reserved.  
    //  
      
    #import "Person.h"  
      
    @implementation Person  
      
    - (void)dealloc  
    {  
        [_name release];  
        [super dealloc];  
    }  
      
      
    //  归档 进行编码  
    - (void)encodeWithCoder:(NSCoder *)aCoder  
    {  
        [aCoder encodeObject:self.name forKey:@"name"];  
        [aCoder encodeInteger:self.age forKey:@"age"];  
        [aCoder encodeObject:self.data forKey:@"data"];  
      
      
    }  
      
    //  反归档 解码  
    - (id)initWithCoder:(NSCoder *)aDecoder  
    {  
        self = [super init];  
        if (self) {  
              
            self.name = [aDecoder decodeObjectForKey:@"name"];  
            self.age = [aDecoder decodeIntegerForKey:@"age"];  
            self.data = [aDecoder decodeObjectForKey:@"data"];  
      
      
              
        }  
        return self;  
    }  
      
      
    @end  


[objc] view plaincopy

    //  归档复杂对象  
    - (void)archive  
    {  
    // 获取Documents文件路径  
    #define kDocumentsPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]  
      
          
        Person *p = [[Person alloc] init];  
        p.name = @"wang";  
        p.age = 12;  
        //   UIImagePNGRepresentation([UIImage imageNamed:@"shen"])  
        //   将png图片 转化成data  
        p.data = UIImagePNGRepresentation([UIImage imageNamed:@"shen"]);  
      
        //  创建一个可变的DATA 进行对 复杂对象编码后的储存  
        NSMutableData *data = [NSMutableData data];  
          
        //  创建归档对象  
        NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
        //  进行归档  
        [archive encodeObject:p forKey:@"person"];  
        //  归档结束  
        [archive finishEncoding];  
          
      
        NSString *archivePath = [kDocumentsPath stringByAppendingPathComponent:@"savePerson"];  
        NSLog(@"%@",archivePath);  
      
        //  写入归档文件  
        [data writeToFile:archivePath atomically:YES];  
        [p release];  
        [archive release];  
          
          
    }  
    //  反归档  
    - (void)unarchive  
    {  
        NSString *archivePath = [kDocumentsPath stringByAppendingPathComponent:@"savePerson"];  
          
        //  按路径读取 归档的data文件  
        NSData *data = [NSData dataWithContentsOfFile:archivePath];  
          
        //  创建反归档对象  
        NSKeyedUnarchiver *aaa = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];  
        //  取出归档的对象  
        Person *p = [aaa decodeObjectForKey:@"person"];  
          
        UIImage *image = [UIImage imageWithData:p.data];  
          
        NSLog(@"%@",p.name);  
    }  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值