你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> objc對象歸檔 序列化

objc對象歸檔 序列化

編輯:IOS開發綜合

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]
------------------------------------------------測試代碼------------------------------------------------  
  
NSString *str = @"hello";  
    [str writeToFile:@"/Users/lili/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];  
      
    NSArray *arr = [[NSArray alloc]initWithObjects:@"lili",@"tata", nil];  
    [arr writeToFile:@"/Users/lili/Desktop/arr.txt" atomically:YES];  
      
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lili",@"男"] forKeys:@[@"name",@"sex"]];  
    [dict writeToFile:@"/Users/lili/Desktop/dict.txt" atomically:YES];  
      
      
    Person *parent = [[Person alloc]init];  
    parent.age = 40;  
    parent.name = @"lili";  
      
    Person *child = [[Person alloc]init];  
    child.age = 22;  
    child.name = @"linian";  
      
    parent.child = child;  
      
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:parent];  
    [data writeToFile:@"/Users/lili/Desktop/data.txt" atomically:YES];  
      
    Person *people = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lili/Desktop/data.txt"];  
    NSLog(@"%@",people);  
      
    NSDictionary *dictPerson = [NSDictionary dictionaryWithObjects:@[[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:child]] forKeys:@[@"parent",@"child"]];  
    [dictPerson writeToFile:@"/Users/lili/Desktop/dictPerson.txt" atomically:YES];  
      
    NSDictionary *pdict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lili/Desktop/dictPerson.txt"];  
    Person *tPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pdict objectForKey:@"parent"]];  
    NSLog(@"%@",tPerson);  
      
      
      
    NSArray *arrPerson = [[NSArray alloc]initWithObjects:[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:parent], nil];  
    [arrPerson writeToFile:@"/Users/lili/Desktop/arrPerson.txt" atomically:YES];  
      
    NSArray *pArr = [NSArray arrayWithContentsOfFile:@"/Users/lili/Desktop/arrPerson.txt"];  
    Person *aPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pArr objectAtIndex:0]];  
    NSLog(@"%@",aPerson);  
  
  
------------------------------------------------Person類------------------------------------------------  
  
#import <Foundation/Foundation.h>  
@interface Person : NSObject<NSCoding>  
@property(strong,nonatomic)Person *child;  
@property(assign,nonatomic)int age;  
@property(copy,nonatomic)NSString *name;  
@end  
  
  
#import "Person.h"  
@implementation Person  
-(NSString *)description  
{  
    return [NSString stringWithFormat:@"{age:%d,name:%@,child:%@}",self.age,self.name,self.child];  
}  
  
-(void)encodeWithCoder:(NSCoder *)aCoder  
{  
    [aCoder encodeInt:self.age forKey:@"age"];  
    [aCoder encodeObject:self.name forKey:@"name"];  
    [aCoder encodeObject:self.child forKey:@"child"];  
}  
  
-(id)initWithCoder:(NSCoder *)aDecoder  
{  
    if(self = [super init]){  
        self.age = [aDecoder decodeIntForKey:@"age"];  
        self.name = [aDecoder decodeObjectForKey:@"name"];  
        self.child = [aDecoder decodeObjectForKey:@"child"];  
    }  
    return self;  
}  
@end  

------------------------------------------------測試代碼------------------------------------------------
 
NSString *str = @"hello";
    [str writeToFile:@"/Users/lili/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    NSArray *arr = [[NSArray alloc]initWithObjects:@"lili",@"tata", nil];
    [arr writeToFile:@"/Users/lili/Desktop/arr.txt" atomically:YES];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lili",@"男"] forKeys:@[@"name",@"sex"]];
    [dict writeToFile:@"/Users/lili/Desktop/dict.txt" atomically:YES];
    
    
    Person *parent = [[Person alloc]init];
    parent.age = 40;
    parent.name = @"lili";
    
    Person *child = [[Person alloc]init];
    child.age = 22;
    child.name = @"linian";
    
    parent.child = child;
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:parent];
    [data writeToFile:@"/Users/lili/Desktop/data.txt" atomically:YES];
    
    Person *people = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lili/Desktop/data.txt"];
    NSLog(@"%@",people);
    
    NSDictionary *dictPerson = [NSDictionary dictionaryWithObjects:@[[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:child]] forKeys:@[@"parent",@"child"]];
    [dictPerson writeToFile:@"/Users/lili/Desktop/dictPerson.txt" atomically:YES];
    
    NSDictionary *pdict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lili/Desktop/dictPerson.txt"];
    Person *tPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pdict objectForKey:@"parent"]];
    NSLog(@"%@",tPerson);
    
    
    
    NSArray *arrPerson = [[NSArray alloc]initWithObjects:[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:parent], nil];
    [arrPerson writeToFile:@"/Users/lili/Desktop/arrPerson.txt" atomically:YES];
    
    NSArray *pArr = [NSArray arrayWithContentsOfFile:@"/Users/lili/Desktop/arrPerson.txt"];
    Person *aPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pArr objectAtIndex:0]];
    NSLog(@"%@",aPerson);
 
 
------------------------------------------------Person類------------------------------------------------
 
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property(strong,nonatomic)Person *child;
@property(assign,nonatomic)int age;
@property(copy,nonatomic)NSString *name;
@end
 
 
#import "Person.h"
@implementation Person
-(NSString *)description
{
    return [NSString stringWithFormat:@"{age:%d,name:%@,child:%@}",self.age,self.name,self.child];
}
 
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInt:self.age forKey:@"age"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.child forKey:@"child"];
}
 
-(id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init]){
        self.age = [aDecoder decodeIntForKey:@"age"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.child = [aDecoder decodeObjectForKey:@"child"];
    }
    return self;
}
@end

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved