归档(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);

    // 将数据归档到NSDictionaryNSMutableDictionary

    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 atomicallyYES];

    

    // // 将数据归档到NSArrayNSMutableArray

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

    [dataArray addObject:@"Tom"];

    [dataArray addObject:@"LiLei"];

    [dataArray addObject:@"HanMeimei"];

    

    NSNumber *number=[[NSNumber allocinitWithInt:100];

    [dataArray addObject:number];

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

    [dataArray writeToFile: arrayName atomicallyYES];

    

    // 从文件中读取数据到NSDictionaryNSMutableDictionary

    NSMutableDictionary *newDictionary=[NSMutableDictionarydictionaryWithContentsOfFile:dictionaryName];

    // 从文件中读取数据到NSArrayNSMutableArray

    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 将数据归档到NSDictionaryNSMutableDictionary

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

    [NSKeyedArchiver archiveRootObject:dataDictionary toFile:dictionaryName];


 

// NSKeyedArchiver 将数据归档到NSArrayNSMutableArray

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了。

问题发现了,原来需要编码/解码操作!还等什么,向编码/解码操作进发!!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章