您的位置:首页 > 其它

归档(archive)文件(一)

2016-01-15 18:19 295 查看
什么是归档?

归档(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了。

问题发现了,原来需要编码/解码操作!还等什么,向编码/解码操作进发!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: