archive文件

参考   archive文件 - 云+社区 - 腾讯云

什么是归档?

归档(archive)就是将数据整理到外部文件(xml,plist,txt 等)!

在object-c支持的可以进行归档的数据类型为:NSDate, NSNumber, NSString, NSArray, or NSDictionary

先看归档代码吧:

- (NSString *)dataFilePath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}

NSString *filePath=[self dataFilePath];

    

    NSLog(@"path:%@",filePath);

    // 将数据归档到NSDictionary或NSMutableDictionary

    NSMutableDictionary *dataDictionary=[[NSMutableDictionary alloc] init];

    [dataDictionary setObject:@"My name is Tom" forKey:@"Tom"];

    [dataDictionary setObject:@"My name is LiLei" forKey:@"LiLei"];

    [dataDictionary setObject:@"My name is HanMeimei" forKey:@"HanMeimei"];

    

    NSString *dictionaryName= [filePath stringByAppendingPathComponent:@"NSMutableDictionary"];

    [dataDictionary writeToFile: dictionaryName atomically: YES];

    

    // // 将数据归档到NSArray或NSMutableArray

    NSMutableArray *dataArray=[[NSMutableArray alloc] init];

    [dataArray addObject:@"Tom"];

    [dataArray addObject:@"LiLei"];

    [dataArray addObject:@"HanMeimei"];

    

    NSNumber *number=[[NSNumber alloc] initWithInt:100];

    [dataArray addObject:number];

    NSString *arrayName= [filePath stringByAppendingPathComponent:@"NSMutableArray"];

    [dataArray writeToFile: arrayName atomically: YES];

    

    // 从文件中读取数据到NSDictionary或NSMutableDictionary中

    NSMutableDictionary *newDictionary=[NSMutableDictionarydictionaryWithContentsOfFile:dictionaryName];

    // 从文件中读取数据到NSArray或NSMutableArray中

    NSMutableArray *newArray=[NSMutableArray arrayWithContentsOfFile:arrayName];

    

    for (NSString *theStr in newDictionary) {

        NSLog(@"----%@",theStr);

    }

    

    for (NSString *theStr in newArray) {

        NSLog(@"----%@",theStr);

    }

我们发现,数据被存储到了指定的文件中,这些文件格式为XML。

并且,可以使用响应的方法将XML文件中的内容读取到响应的数据中。

object-c还提供了其他的归档方式。

我们还可以用NSKeyedArchiver方式来进行归档,用NSKeyedUnarchiver相应的方式进行反归档。

贴代码:

// NSKeyedArchiver 将数据归档到NSDictionary或NSMutableDictionary

NSString *dictionaryName= [filePath stringByAppendingPathComponent:@"ArchiverDictionary.archiver"];

    [NSKeyedArchiver archiveRootObject:dataDictionary toFile:dictionaryName];

// NSKeyedArchiver 将数据归档到NSArray或NSMutableArray

NSString *arrayName= [filePath stringByAppendingPathComponent:@"ArchiverArray.archiver"];

    [NSKeyedArchiver archiveRootObject:dataArray toFile:arrayName];

// NSKeyedUnarchiver

    NSMutableDictionary *newDictionary=[NSKeyedUnarchiverunarchiveObjectWithFile:dictionaryName];

    NSMutableArray *newArray=[NSKeyedUnarchiver unarchiveObjectWithFile:arrayName];

如果,我们将自己定义的类对象,进行归档存储到文件中,那该多好!

于是,我们开始归档自己的类。

NSMutableArray *objArray=[NSMutableArray arrayWithObjects:

                              card1,

                              card2,

                              card3,

                              card4,

                              nil];

    

    

    NSString *arrayName=[filePath stringByAppendingPathComponent:@"ObjectFile"];

    

    BOOL isSuccess= [objArray writeToFile:arrayName atomically:YES];

    

    if (isSuccess) {

        NSLog(@"Success");

    }else{

        NSLog(@"False");

    }

   

    isSuccess= [NSKeyedArchiver archiveRootObject:objArray toFile:arrayName];

    if (isSuccess) {

        NSLog(@"Success");

    }else{

        NSLog(@"False");

    }

很遗憾,我们没有成功!甚至报错了!

'NSInvalidArgumentException', reason: '-[AddressCard encodeWithCoder:]: unrecognized selector sent to instance 0x1188ef60'

原来,默认情况下,只能对NSDate, NSNumber, NSString, NSArray, or NSDictionary来进行归档。

如果要归档我们自定义的对象,程序不知道到如何进行编码/解码操作!所以就Error了。

问题发现了,原来需要编码/解码操作!还等什么,向编码/解码操作进发!!!

NSCoding协议-编码/解码

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wanderer001

ROIAlign原理

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值