归档(archive)文件(一)
来源:互联网 发布:wayuu图案设计软件 编辑:程序博客网 时间:2024/06/06 03:31
http://blog.sina.com.cn/s/blog_7b9d64af01019kab.html
什么是归档?
归档(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=[NSMutableDictionary dictionaryWithContentsOfFile: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=[NSKeyedUnarchiver unarchiveObjectWithFile: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协议-编码/解码
- 归档(archive)文件(一)
- 归档(archive)文件(一)
- 归档(archive)文件(一)
- 归档(archive)文件(二)
- Oracle中的归档日志(Archive Log)
- oracle归档日志(archive log)
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- iOS archive(归档)的总结
- vb硬盘及主板
- angular.js,IE7,8,9兼容性的处理
- C语言指针和数组基础
- [Win32] 直接读写磁盘扇区(磁盘绝对读写)
- 护肤小窍门
- 归档(archive)文件(一)
- 探索安卓中有意义的动画!
- error while loading shared libraries: libpcap.so.1 解决方法
- speex编解码在android上实现
- S5PV210 裸机开发驱动之LED灯
- AT LINE SELECTION的用法
- CoOS使用教程——任务的状态
- tomcat连接池配置详解
- awakeFromNib初始化方法和Nib文件