20150623_OC之文件归档NSKeyedArchiver

本文详细介绍了Objective-C中的归档与反归档技术,包括如何使用NSKeyedArchiver和NSKeyedUnarchiver进行对象数据的存储与恢复。通过实例展示了对象数据的归档过程和反归档过程,以及手动归档和反归档的方法。
摘要由CSDN通过智能技术生成

文件归档NSKeyedArchiver,可以实现对文件的归档,序列化,反归档NSKeyedUnarchiver

//
//  main.m
//  IOS150623_objectiveC_归档
//
//  Created by PengJunlong on 15/6/23.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

//*************************
//*         归档          *
//*************************

//归档:把对象数据存储到文件的过程,不能归档自定义的类型(转化成字节码存储)
//反归档:从文件中读取数据到对象的过程

int main(int argc, const char * argv[]) {
    @autoreleasepool {

//#if 0
        //-----------------用系统方法归档------------------
        //方法一:
        NSMutableDictionary *mulDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
        [mulDict setValue:[NSDate date] forKey:@"date"];
        [mulDict setValue:[NSNumber numberWithInt:232] forKey:@"num"];
        [mulDict setValue:[@"IOSApple" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"];
        
        BOOL ret = [NSKeyedArchiver archiveRootObject:mulDict toFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/arch.txt"];
        if (ret) {
            NSLog(@"归档成功");
        }
        else
        {
            NSLog(@"归档失败");
        }
        
        //方法二:
        NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:mulDict];
        ret = [data1 writeToFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/archdata.txt" atomically:YES];
        if (ret) {
            NSLog(@"归档data成功");
        }
        else
        {
            NSLog(@"归档data失败");
        }
//#else
        //反归档
        //方法一:
        NSMutableDictionary *mulDict1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/arch.txt"];
        if (mulDict1) {
            NSLog(@"mulDict1 = %@",mulDict1);
        }
        
        //方法二:
        NSData *da = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/archdata.txt"];
        NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:da];
        if (dic) {
            NSLog(@"dic = %@",dic);
        }
        
//#endif
        
        //-------------手动归档------------------
        //归档
        //创建一个空的可变data对象,一定是可变的
        NSMutableData *data = [NSMutableData data];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        NSArray *array = [NSArray arrayWithObjects:@"one",@"two",[NSNumber numberWithInt:23],[NSDate date],[@"I love china" dataUsingEncoding:NSUTF8StringEncoding], nil];
        
        NSMutableDictionary *dictionary1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"5",@"five",@"6",@"six",[NSNumber numberWithFloat:23.33],@"numfloat",[NSNumber numberWithInt:233],@"num", nil];
        
        //对数组及子字典进行编码
        [archiver encodeObject:array forKey:@"array"];
        [archiver encodeObject:dictionary1 forKey:@"dictionary"];
        //完成编码,一点要finishEncoding
        [archiver finishEncoding];
        
        //写入文件
        int ret1 = [data writeToFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/manualarch.txt" atomically:YES];
        if (ret1) {
            NSLog(@"文件归档成功");
        }
        else
        {
            NSLog(@"归档失败");
        }
        
        //反归档
        NSData *data2 = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_objectiveC_归档/manualarch.txt"];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
        NSArray *array1 = [unarchiver decodeObjectForKey:@"array"];
        NSMutableDictionary *dictionary2 = [unarchiver decodeObjectForKey:@"dictionary"];
        
        //完成解码
        [unarchiver finishDecoding];
        
        NSLog(@"Array= %@",array1);
        NSLog(@"==================");
        NSLog(@"Dictionary = %@",dictionary2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值