NSKeyedArchive详解

         基本的数据类型如NSString、NSDictionary、NSArray、NSData、NSNumber等可以用属性列表的方法持久化到.plist 文件中,但如果是一些自定义的类的话,属性列表的方法就不管用了。archiver 方法可以做到。

编码如下:

     首先新建一个person类,定义它的三个属性,如下:

 

[cpp]  view plain copy print ?
 
  1. //  
  2. //  person.h  
  3. //  数据持久化之archiver  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface person : UIView<NSCoding>  
  12. @property(nonatomic, assign) int age;  
  13. @property(nonatomic, copy)NSString *name;  
  14. @property(nonatomic, assign)float height;  
  15. @end  

 

[cpp]  view plain copy print ?
 
  1. //  
  2. //  person.m  
  3. //  数据持久化之archiver  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "person.h"  
  10.   
  11. @implementation person  
  12.   
  13.   
  14. #pragma mark 写入文件  
  15. -(void)encodeWithCoder:(NSCoder *)encoder{  
  16.     [super encodeWithCoder:encoder];//不要忘了这个  
  17.     [encoder encodeInt:self.age forKey:@"age"];  
  18.     [encoder encodeObject:self.name forKey:@"name"];  
  19.     [encoder encodeFloat:self.height forKey:@"height"];  
  20. }  
  21.   
  22. #pragma mark 从文件中读取  
  23. -(id)initWithCoder:(NSCoder *)decoder{  
  24.     self = [super initWithCoder:decoder];//不要忘了这个  
  25.     self.age = [decoder decodeIntForKey:@"age"];  
  26.     self.name = [decoder decodeObjectForKey:@"name"];  
  27.     self.height = [decoder decodeFloatForKey:@"height"];  
  28.       
  29.       
  30.     return self;  
  31. }  
  32.   
  33.   
  34. -(NSString *)description{  
  35.     return [NSString stringWithFormat:@"name = %@, age = %d, height = %f",self.name,self.age,self.height];  
  36. }  
  37.   
  38. //释放资源  
  39. -(void)dealloc{  
  40.     [super dealloc];  
  41.     [_name release];  
  42. }  
  43. @end  


然后再ViewController.m文件中写如下代码:

 

 

[cpp]  view plain copy print ?
 
  1. //  
  2. //  ViewController.m  
  3. //  数据持久化之archiver  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #import "person.h"  
  11.   
  12. @interface ViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation ViewController  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     [self createPerson];  
  22.     [self readPerson];  
  23. }  
  24.   
  25. //创建  
  26. -(void)createPerson{  
  27.       
  28.     person *p = [[[person alloc] init] autorelease];  
  29.     p.age = 20;  
  30.     p.name = @"Rio";  
  31.     p.height =1.75f;  
  32.       
  33.     //获得Document的路径  
  34.     NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  
  35.     NSString *path = [documents stringByAppendingPathComponent:@"person.archiver"];//拓展名可以自己随便取  
  36.       
  37.     [NSKeyedArchiver archiveRootObject:p toFile:path];  
  38.       
  39. }  
  40.   
  41. //读取  
  42. -(void)readPerson{  
  43.     NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  
  44.     NSString *path = [documents stringByAppendingPathComponent:@"person.archiver"];  
  45.     person *person1 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];  
  46.     NSLog(@"%@",person1);  
  47. }  
  48.   
  49. @end  


,,在写ViewController.m文件代码的时候,必须在头文件中遵循NSCoding协议。

 

 

[cpp]  view plain copy print ?
 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<NSCoding>  
  4.   
  5. @end  

 

运行结果如下:

 

 

2013-09-22 13:31:39.509 数据持久化之archiver[1080:c07] name = Rio, age = 20, height = 1.750000

 

注意事项:

 

 

转载自:http://blog.csdn.net/chaoyuan899/article/details/11895299

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值