归档(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了。
问题发现了,原来需要编码/解码操作!还等什么,向编码/解码操作进发!!!
归档(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了。
问题发现了,原来需要编码/解码操作!还等什么,向编码/解码操作进发!!!
相关文章推荐
- Unity 出现 ssl peer certificate or ssh remote key
- java 类的继承体系
- 坑你没商量之c#微信回调中核实订单的签名验证
- 公钥和私钥的使用机制
- nw.js FrameLess Window下的窗口拖拽与窗口大小控制
- Nine-Patch格式图片浅析
- C++语言学习资料大全分享 想要的速度下
- android 悬浮覆盖状态栏的相关建议
- linux系统shell语法简介-49
- 开源日志系统比较:scribe、chukwa、kafka、flume
- JSQMessagesViewController (一)
- 使用DES方式,加、解密字符
- EJB invocations from a remote client using JNDI
- 启动eclipse出现错误提示:an error hava occured,see the log
- QT全平台设置图标,全平台静态编译 good
- 编译内核模块竟然内存不够用
- 单例模式
- 使用rand替换random模块
- Red Hat 和Cent OS 低版本安装mysql
- fragment