对象归档

NSString、NSArray、NSData、NSDictionary都实现了NSCoding协议,可直接通过调用writeToFile归档,那么OBJC自定义对象类型呢?首先实现NSCoding协议,重写encodeWithCode方法和initWithCode方法,然后通过NSKeyedArchiver转换为NSData,然后通过NSData的writeToFile方法写入到文件,或者将转换后的NSData放入到NSArray或NSDictionary中调用writeToFile写入到文件便可实现包装了自定义类型的数据和字典的归档;通过NSKeyedUnarchiver读取归档文件到对象,或者通过NSArray的arrrayWithContentsOfFile或NSDictionary的dictionaryWithContentsOfFile到数组对象或字典,然后取出序列化过的自定义对象(即自定义对象的NSData形式),然后通过NSKeyedUnarchiver反归档到对象。

[plain]  view plain copy
  1. ------------------------------------------------测试代码------------------------------------------------   
  2.    
  3. NSString *str = @"hello";   
  4.     [str writeToFile:@"/Users/lili/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];   
  5.        
  6.     NSArray *arr = [[NSArray alloc]initWithObjects:@"lili",@"tata", nil];   
  7.     [arr writeToFile:@"/Users/lili/Desktop/arr.txt" atomically:YES];   
  8.        
  9.     NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lili",@"男"] forKeys:@[@"name",@"sex"]];   
  10.     [dict writeToFile:@"/Users/lili/Desktop/dict.txt" atomically:YES];   
  11.        
  12.        
  13.     Person *parent = [[Person alloc]init];   
  14.     parent.age = 40;   
  15.     parent.name = @"lili";   
  16.        
  17.     Person *child = [[Person alloc]init];   
  18.     child.age = 22;   
  19.     child.name = @"linian";   
  20.        
  21.     parent.child = child;   
  22.        
  23.     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:parent];   
  24.     [data writeToFile:@"/Users/lili/Desktop/data.txt" atomically:YES];   
  25.        
  26.     Person *people = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lili/Desktop/data.txt"];   
  27.     NSLog(@"%@",people);   
  28.        
  29.     NSDictionary *dictPerson = [NSDictionary dictionaryWithObjects:@[[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:child]] forKeys:@[@"parent",@"child"]];   
  30.     [dictPerson writeToFile:@"/Users/lili/Desktop/dictPerson.txt" atomically:YES];   
  31.        
  32.     NSDictionary *pdict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lili/Desktop/dictPerson.txt"];   
  33.     Person *tPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pdict objectForKey:@"parent"]];   
  34.     NSLog(@"%@",tPerson);   
  35.        
  36.        
  37.        
  38.     NSArray *arrPerson = [[NSArray alloc]initWithObjects:[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:parent], nil];   
  39.     [arrPerson writeToFile:@"/Users/lili/Desktop/arrPerson.txt" atomically:YES];   
  40.        
  41.     NSArray *pArr = [NSArray arrayWithContentsOfFile:@"/Users/lili/Desktop/arrPerson.txt"];   
  42.     Person *aPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pArr objectAtIndex:0]];   
  43.     NSLog(@"%@",aPerson);   
  44.    
  45.    
  46. ------------------------------------------------Person类------------------------------------------------   
  47.    
  48. #import <Foundation/Foundation.h>   
  49. @interface Person : NSObject<NSCoding>   
  50. @property(strong,nonatomic)Person *child;   
  51. @property(assign,nonatomic)int age;   
  52. @property(copy,nonatomic)NSString *name;   
  53. @end   
  54.    
  55.    
  56. #import "Person.h"   
  57. @implementation Person   
  58. -(NSString *)description   
  59. {   
  60.     return [NSString stringWithFormat:@"{age:%d,name:%@,child:%@}",self.age,self.name,self.child];   
  61. }   
  62.    
  63. -(void)encodeWithCoder:(NSCoder *)aCoder   
  64. {   
  65.     [aCoder encodeInt:self.age forKey:@"age"];   
  66.     [aCoder encodeObject:self.name forKey:@"name"];   
  67.     [aCoder encodeObject:self.child forKey:@"child"];   
  68. }   
  69.    
  70. -(id)initWithCoder:(NSCoder *)aDecoder   
  71. {   
  72.     if(self = [super init]){   
  73.         self.age = [aDecoder decodeIntForKey:@"age"];   
  74.         self.name = [aDecoder decodeObjectForKey:@"name"];   
  75.         self.child = [aDecoder decodeObjectForKey:@"child"];   
  76.     }   
  77.     return self;   
  78. }   
  79. @end   

你可能感兴趣的:(对象归档)