文件归档

//
//  main.m
//  OC5-文件归档
//
//  Created by qianfeng on 15/6/23.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <Foundation/Foundation.h>
//归档:把对象数据存储到文件的过程.
//反归档:从文件中读取数据到对象中的过程.
int main(int argc, const char * argv[]) {
    @autoreleasepool {
#if 0
        //用系统方法归档
        //NSKeyedArchiver
        NSMutableDictionary *mutdic =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];
        [mutdic setValue:[NSDate date] forKey:@"date"];
        [mutdic setValue:[NSNumber numberWithInt:123] forKey:@"num"];
        [mutdic setValue:[@"ios" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"];
        bool ret =[NSKeyedArchiver archiveRootObject:mutdic toFile:@"/Users/qianfeng/Desktop/text/arch.text"];//文件不存在自动创建.
        if(ret)
        {
            NSLog(@"文件归档成功");
            
        }
        else{
            NSLog(@"文件归档失败");
        }
//#else
        //反归档
        NSDictionary *dict =[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/qianfeng/Desktop/text/arch.text"];
        if (dict) {
              NSLog(@"%@",dict);
        }
        
        
#endif
        
#if 1
        //归档
        
        NSMutableDictionary *mutdic =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];
        [mutdic setValue:[NSDate date] forKey:@"date"];
        [mutdic setValue:[NSNumber numberWithInt:123] forKey:@"num"];
        [mutdic setValue:[@"ios" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"];
        NSData *data =[NSKeyedArchiver archivedDataWithRootObject:mutdic];
        //data =[NSKeyedArchiver archivedDataWithRootObject:mutdic];
        BOOL ret =[data writeToFile:@"/Users/qianfeng/Desktop/text/data.text" atomically:YES];
        
        if(ret)
        {
            NSLog(@"文件归档成功");
            
        }
        else{
            NSLog(@"文件归档失败");
        }
        
#else
      
        //反归档
        NSData *data =[NSData dataWithContentsOfFile:@"/Users/qianfeng/Desktop/text/data.text"];
        NSDictionary *dict =[NSKeyedUnarchiver unarchiveObjectWithData:data];
        if(dict)
        {
            NSLog(@"%@",dict);
        }
        
        
#endif
        
#if 0
        //手动归档
        //创建一个空的可变data对象
        NSMutableData *data =[NSMutableData data];//一定是可变的二进制对象(NSMutableData)
        NSKeyedArchiver *archiver =[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        NSArray *array =[NSArray arrayWithObjects:@"one",@"two",[NSNumber numberWithInt:123],[NSDate date],[@"ios" dataUsingEncoding:NSUTF8StringEncoding], nil];
        
        NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"5",@"five",@"6",@"six",[NSNumber numberWithInt:123],@"num1",[NSNumber numberWithFloat:23.1],@"num2", nil];
        //对数组及字典进行编码
        [archiver encodeObject:array forKey:@"array"];
        [archiver encodeObject:dict forKey:@"dictionary"];
        
        //完成编码
        [archiver finishEncoding];
        
        //写入文件
       int ret=  [data writeToFile:@"/Users/qianfeng/Desktop/text/data.text" atomically:YES];
        if (ret) {
            NSLog(@"文件归档成功");
            
        }
        else{
            NSLog(@"文件归档失败");
        }
        
        
//#else
        //解归档
        NSData *data =[NSData dataWithContentsOfFile:@"/Users/qianfeng/Desktop/text/data.text"];
        NSKeyedUnarchiver *unarchiver =[[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        NSArray *array =[unarchiver decodeObjectForKey:@"array"];
        NSDictionary *dict =[unarchiver decodeObjectForKey:@"dictionary"];
        //完成解码
        [unarchiver finishDecoding];
        
        NSLog(@"array =%@",array);
        NSLog(@"----------------");
        NSLog(@"dict =%@",dict);
        
#endif
    }
    return 0;
    
}

 

posted @ 2015-06-23 12:34  sirzhang  阅读(263)  评论(0编辑  收藏  举报
AmazingCounters.com