iOS对象序列化 .

系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了。

我在这里主要介绍自定义对象的归档。


Sample.h文件

  1. //  
  2. //  Sample.h  
  3. //  Serialization  
  4. //  
  5. //  Created by 周 敏 on 12-11-1.  
  6. //  Copyright (c) 2012年 周 敏. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Sample : NSObject<NSCoding> {  
  12.       
  13.     NSString* name;  
  14.     int magicNumber;  
  15.     float shoeSize;  
  16.     NSMutableArray *subThingies;  
  17. }  
  18.   
  19. @property(copy) NSString* name;  
  20. @property int magicNumber;  
  21. @property float shoeSize;  
  22. @property (retain) NSMutableArray *subThingies;  
  23.   
  24.   
  25. -(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float) ss;  
  26.   
  27. @end  
//
//  Sample.h
//  Serialization
//
//  Created by 周 敏 on 12-11-1.
//  Copyright (c) 2012年 周 敏. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Sample : NSObject<NSCoding> {
    
	NSString* name;
	int magicNumber;
	float shoeSize;
	NSMutableArray *subThingies;
}

@property(copy) NSString* name;
@property int magicNumber;
@property float shoeSize;
@property (retain) NSMutableArray *subThingies;


-(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float) ss;

@end


Sample.m文件

  1. //  
  2. //  Sample.m  
  3. //  Serialization  
  4. //  
  5. //  Created by 周 敏 on 12-11-1.  
  6. //  Copyright (c) 2012年 周 敏. All rights reserved.  
  7. //  
  8.   
  9. #import "Sample.h"  
  10.   
  11. @implementation Sample  
  12.   
  13. @synthesize name;  
  14. @synthesize magicNumber;  
  15. @synthesize shoeSize;  
  16. @synthesize subThingies;  
  17.   
  18. -(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float)ss  
  19. {  
  20.     if (self=[super init])  
  21.     {  
  22.         self.name = n;  
  23.         self.magicNumber = m;  
  24.         self.shoeSize = ss;  
  25.         self.subThingies = [NSMutableArray array];  
  26.     }  
  27.     return (self);  
  28. }  
  29.   
  30. -(void) dealloc  
  31. {  
  32.     [name release];  
  33.     [subThingies release];  
  34.     [super dealloc];  
  35. }  
  36.   
  37. //将对象编码(即:序列化)  
  38. -(void) encodeWithCoder:(NSCoder *)aCoder  
  39. {  
  40.     [aCoder encodeObject:name forKey:@"name"];  
  41.     [aCoder encodeInt:magicNumber forKey:@"magicNumber"];  
  42.     [aCoder encodeFloat:shoeSize forKey:@"shoeSize"];  
  43.     [aCoder encodeObject:subThingies forKey:@"subThingies"];  
  44. }  
  45.   
  46. //将对象解码(反序列化)  
  47. -(id) initWithCoder:(NSCoder *)aDecoder  
  48. {  
  49.     if (self=[super init])  
  50.     {  
  51.         self.name = [aDecoder decodeObjectForKey:@"name"];  
  52.         self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];  
  53.         self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];  
  54.         self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];  
  55.     }  
  56.     return (self);  
  57.       
  58. }  
  59.   
  60.   
  61. -(NSString*) description  
  62. {  
  63.     NSString *description = [NSString stringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThingies];  
  64.     return (description);  
  65. }  
  66.   
  67. @end  
//
//  Sample.m
//  Serialization
//
//  Created by 周 敏 on 12-11-1.
//  Copyright (c) 2012年 周 敏. All rights reserved.
//

#import "Sample.h"

@implementation Sample

@synthesize name;
@synthesize magicNumber;
@synthesize shoeSize;
@synthesize subThingies;

-(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float)ss
{
	if (self=[super init])
	{
		self.name = n;
		self.magicNumber = m;
		self.shoeSize = ss;
		self.subThingies = [NSMutableArray array];
	}
	return (self);
}

-(void) dealloc
{
	[name release];
	[subThingies release];
	[super dealloc];
}

//将对象编码(即:序列化)
-(void) encodeWithCoder:(NSCoder *)aCoder
{
	[aCoder encodeObject:name forKey:@"name"];
	[aCoder encodeInt:magicNumber forKey:@"magicNumber"];
	[aCoder	encodeFloat:shoeSize forKey:@"shoeSize"];
	[aCoder	encodeObject:subThingies forKey:@"subThingies"];
}

//将对象解码(反序列化)
-(id) initWithCoder:(NSCoder *)aDecoder
{
	if (self=[super init])
	{
		self.name = [aDecoder decodeObjectForKey:@"name"];
		self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];
		self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];
		self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];
	}
	return (self);
	
}


-(NSString*) description
{
	NSString *description = [NSString stringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThingies];
	return (description);
}

@end

使用模版

  1. NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];  
  2. Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];      
  3. [s1.subThingies addObject:@"1"];    
  4. [s1.subThingies addObject:@"2"];    
  5. //序列化    
  6. NSData  *data1 = [NSKeyedArchiver archivedDataWithRootObject:s1];//将s1序列化后,保存到NSData中    
  7. [s1 release];    
  8. [data1 writeToFile:path atomically:YES];//持久化保存成物理文件    
  9. //反序列化    
  10. NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件     
  11. Sample *s2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化       
  12. NSLog(@"%@",s2);    
NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];
Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];    
[s1.subThingies addObject:@"1"];  
[s1.subThingies addObject:@"2"];  
//序列化  
NSData  *data1 = [NSKeyedArchiver archivedDataWithRootObject:s1];//将s1序列化后,保存到NSData中  
[s1 release];  
[data1 writeToFile:path atomically:YES];//持久化保存成物理文件  
//反序列化  
NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件   
Sample *s2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化     
NSLog(@"%@",s2);  


如果是多个这类对象组成的数组,序列化也很简单,只须对这个数组进行序列化。

  1. Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];    
  2. [s1.subThingies addObject:@"1"];    
  3. [s1.subThingies addObject:@"2"];    
  4.        
  5. Sample *s2 = [[Sample alloc] initWithName:@"thing2" magicNumber:22 shoeSize:22.2];    
  6. [s2.subThingies addObject:@"22"];    
  7. [s2.subThingies addObject:@"22"];    
  8.        
  9. NSArray *array = [NSArray arrayWithObjects:s1, s2, nil];   
  10. [s1 release];  
  11. [s2 release];  
  12.   
  13. NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];  
  14. //序列化    
  15. NSData  *data1 = [NSKeyedArchiver archivedDataWithRootObject:array];  
  16. [data1 writeToFile:path atomically:YES];//持久化保存成物理文件    
  17. //房序列化    
  18. NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件    
  19. NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化    
  20. NSLog(@"%@",array2);   
Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];  
[s1.subThingies addObject:@"1"];  
[s1.subThingies addObject:@"2"];  
     
Sample *s2 = [[Sample alloc] initWithName:@"thing2" magicNumber:22 shoeSize:22.2];  
[s2.subThingies addObject:@"22"];  
[s2.subThingies addObject:@"22"];  
     
NSArray *array = [NSArray arrayWithObjects:s1, s2, nil]; 
[s1 release];
[s2 release];

NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];
//序列化  
NSData  *data1 = [NSKeyedArchiver archivedDataWithRootObject:array];
[data1 writeToFile:path atomically:YES];//持久化保存成物理文件  
//房序列化  
NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件  
NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化  
NSLog(@"%@",array2); 




原处:http://www.cnblogs.com/yjmyzz/archive/2011/03/03/1969859.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值