歸檔(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了。

問題發現了,原來需要編碼/解碼操作!還等什麼,向編碼/解碼操作進發!!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章