【IPHONE开发-OBJECTC入门学习】对象的归档和解归档

转自:http://blog.csdn.net/java886o/article/details/9046967

  1. #import <Foundation/Foundation.h>   
  2. #import "Person.h"   
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.       
  7.     @autoreleasepool {  
  8.           
  9.         //-----------第1种归档方式---------   
  10.           
  11.         //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)   
  12.         NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil];  
  13.           
  14.         NSString* userPath = NSHomeDirectoryForUser(@"3g2win");  
  15.           
  16.         NSLog(@"userPath=%@",userPath);  
  17.           
  18.         NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"];  
  19.           
  20.         NSLog(@"bakFilePath=%@",bakFilePath);  
  21.           
  22.         NSLog(@"归档前的数组:\n\n%@",arrays);  
  23.           
  24.           
  25.         //归档   
  26.           
  27.         if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){  
  28.             NSLog(@"归档成功...\n\n");  
  29.         }else {  
  30.             NSLog(@"归档失败...\n\n");  
  31.         }  
  32.           
  33.           
  34.           
  35.           
  36.         //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)   
  37.         //解归档   
  38.         NSArray* srcArr =  [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath];  
  39.           
  40.         NSLog(@"解归档后的数组:\n\n%@",srcArr);  
  41.           
  42.           
  43.           
  44.         //------------第2种归档方式---------   
  45.           
  46.         //归档   
  47.         NSMutableData* data = [NSMutableData alloc];  
  48.         NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];  
  49.         [archiver initForWritingWithMutableData:data];  
  50.         [archiver encodeObject:@"张三" forKey:@"name"];  
  51.         [archiver encodeInt:25 forKey:@"age"];  
  52.         [archiver encodeFloat:5200.5F forKey:@"money"];  
  53.         [archiver finishEncoding];  
  54.         [data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES];  
  55.           
  56.         //解归档   
  57.         NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];  
  58.         NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];  
  59.         [unArchiver initForReadingWithData:data2];  
  60.           
  61.         NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]);  
  62.           
  63.         NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]);  
  64.           
  65.         NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]);  
  66.           
  67.         //-------------自定义对象的归档和解归档------------   
  68.         Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18];  
  69.           
  70.         [zhao6 display];  
  71.           
  72.         //归档   
  73.         [NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"];  
  74.           
  75.         //解档   
  76.           
  77.         Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"];  
  78.           
  79.         [newZhao6 display];  
  80.           
  81.     }  
  82.     return 0;  
  83. }  
#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        //-----------第1种归档方式---------
        
        //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)
        NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil];
        
        NSString* userPath = NSHomeDirectoryForUser(@"3g2win");
        
        NSLog(@"userPath=%@",userPath);
        
        NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"];
        
        NSLog(@"bakFilePath=%@",bakFilePath);
        
        NSLog(@"归档前的数组:\n\n%@",arrays);
        
        
        //归档
        
        if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){
            NSLog(@"归档成功...\n\n");
        }else {
            NSLog(@"归档失败...\n\n");
        }
        
        
        
        
        //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)
        //解归档
        NSArray* srcArr =  [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath];
        
        NSLog(@"解归档后的数组:\n\n%@",srcArr);
        
        
        
        //------------第2种归档方式---------
        
        //归档
        NSMutableData* data = [NSMutableData alloc];
        NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];
        [archiver initForWritingWithMutableData:data];
        [archiver encodeObject:@"张三" forKey:@"name"];
        [archiver encodeInt:25 forKey:@"age"];
        [archiver encodeFloat:5200.5F forKey:@"money"];
        [archiver finishEncoding];
        [data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES];
        
        //解归档
        NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];
        NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];
        [unArchiver initForReadingWithData:data2];
        
        NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]);
        
        NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]);
        
        NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]);
        
        //-------------自定义对象的归档和解归档------------
        Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18];
        
        [zhao6 display];
        
        //归档
        [NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"];
        
        //解档
        
        Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"];
        
        [newZhao6 display];
        
    }
    return 0;
}



 

Person.h

 

  1. #import <Foundation/Foundation.h>   
  2.   
  3. //必须实现NSCoding协议才能够归档解归档自定义类   
  4.   
  5. @interface Person : NSObject <NSCoding>  
  6.   
  7. {  
  8.     NSString* name;  
  9.     int age;  
  10. }  
  11.   
  12. @property (nonatomic,assign) NSString* name;  
  13. @property (nonatomic,assign) int age;  
  14.   
  15. - (id) initWithName:(NSString*) _name withAge:(int) _age;  
  16.   
  17. - (void) display;  
  18.   
  19.   
  20. @end  
#import <Foundation/Foundation.h>

//必须实现NSCoding协议才能够归档解归档自定义类

@interface Person : NSObject <NSCoding>

{
    NSString* name;
    int age;
}

@property (nonatomic,assign) NSString* name;
@property (nonatomic,assign) int age;

- (id) initWithName:(NSString*) _name withAge:(int) _age;

- (void) display;


@end


Person.m

 

 

 

    1. #import "Person.h"   
    2.   
    3. #define NAME @"NAME"   
    4. #define AGE @"AGE"   
    5.   
    6. @implementation Person  
    7.   
    8. @synthesize name;  
    9. @synthesize age;  
    10.   
    11. - (id) initWithName:(NSString*) _name withAge:(int) _age {  
    12.     if (self = [super init]) {  
    13.         self.name = _name;  
    14.         self.age = _age;  
    15.     }  
    16.     return self;  
    17. }  
    18.   
    19. - (void) display {  
    20.     NSLog(@"Person Name : %@\t Age :%d",name,age);  
    21. }  
    22.   
    23. //归档编码   
    24. - (void)encodeWithCoder:(NSCoder *)aCoder {  
    25.     [aCoder encodeObject:name forKey:NAME];  
    26.     [aCoder encodeInt:age forKey:AGE];  
    27. }  
    28.   
    29. //解归档解码   
    30. - (id)initWithCoder:(NSCoder *)aDecoder {  
    31.     if (self = [super init]) {  
    32.         self.name = [aDecoder decodeObjectForKey:NAME];  
    33.         self.age = [aDecoder decodeIntForKey:AGE];  
    34.     }  
    35.     return self;  
    36. }  
    37.   
    38. @end  

转载于:https://www.cnblogs.com/LCGIS/p/3290456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值