IOS的归档
来源:互联网 发布:现货黄金分时抛吸源码 编辑:程序博客网 时间:2024/06/06 08:47
归档也叫序列化,是将文件存在硬盘,解档是从硬盘还原
5种方式:
第一种、使用属性列表进行归档
如果对象是NSString,NSDictionary,NSArray,NSData或者NSNumber,可以使用writeToFile:atomically方法将数据写到文件,注意这种方式是明文
sample:
NSArray *array = @[@"abc",@"123",@23.4];
if ([array writeToFile:@"text.plist" atomically:YES])
{
NSLog(@"success");
}
NSArray *arr2=[NSArray arrayWithContentsOfFile:@"text.plist"];
NSLog(@"%@",arr2);
NSArray *array =[NSArray arrayWithObjects:
@"Hefeweizen", @"IPA", @"Pilsner", @"Stout",nil];
NSDictionary *dictionary =[NSDictionary dictionaryWithObjectsAndKeys:
array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA",
@"hoppy",nil];
// 得到documents directory的路径
NSString *arrPath = [NSHomeDirectory() stringByAppendingPathComponent:@"arr.plist"];
NSString *dicPath=[NSHomeDirectory() stringByAppendingPathComponent:@"dic.plist"];
// 保存array
[array writeToFile:arrPath atomically:YES];
// 保存dictionary
[dictionary writeToFile:dicPath atomically:YES];
// 从文件中读取回来
NSArray *arr=[NSArray arrayWithContentsOfFile:arrPath];
NSLog(@"arr is %@",arr);
NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:dicPath];
NSLog(@"dic is %@",dic);
第二、NSKeyedArchiver--对象归档,数据会加密
1、对于NSArray或者NSDictionary sample code:
/***归档对象****/
NSArray *array = @[@"abc",@"123",@23.4];
NSString *homePath = NSHomeDirectory();
NSString *path = [homePath stringByAppendingPathComponent:@"test.arc"];
// BOOL success = [NSKeyedArchiver archiveRootObject:array toFile:path];
BOOL success=[NSKeyedArchiver archiveRootObject:array toFile:path];
if (success) {
NSLog(@"archive success");
}
/***解归档****/
NSArray *array2 =[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@",array2);
结果:
success
2013-12-28 22:14:25.353 ArchiverDemo1[1206:303] (
abc,
123,
"23.4"
)
2、如果是其他类型的对象存储到文件,可以利用NSKeyedArchiver类创建带键的档案来完成
NSString *homePath = NSHomeDirectory();
NSString *path = [homePath stringByAppendingPathComponent:@"archiver2.archiv"];
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSArray *array = @[@"jack",@"tom"];
[archiver encodeInt:100 forKey:@"age"];
[archiver encodeObject:array forKey:@"names"];
[archiver finishEncoding];
[archiver release];
BOOL success = [data writeToFile:path atomically:YES];
if (success) {
NSLog(@"archive success");
}
/***解归档对象**/
NSData *data2 = [NSData dataWithContentsOfFile:path];
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
int age = [unArchiver decodeIntForKey:@"age"];
NSArray *names = [unArchiver decodeObjectForKey:@"names"];
[unArchiver release];
NSLog(@"age=%d,names=%@",age,names);
3、自定义对象进行归档,需要实现归档协议NSCoding两个方法
对属性编码,归档的时候会调用
- (void)encodeWithCoder:(NSCoder *)aCoder
//对属性解码,解归档调用
- (id)initWithCoder:(NSCoder *)aDecoder
#import <Foundation/Foundation.h>
@interface user : NSObject <NSCoding>
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *email;
@property(nonatomic,retain)NSString *pwd;
@property(nonatomic,assign)int age;
@end
#import "user.h"
#define AGE @"age"
#define NAME @"name"
#define EMAIL @"email"
#define PASSWORD @"password"
@implementation user
//对属性编码
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:_ageforKey:@"age"];
[aCoder encodeObject:_nameforKey:AGE];
[aCoder encodeObject:_emailforKey:EMAIL];
[aCoder encodeObject:_pwdforKey:PASSWORD];
}
//对属性解码
- (id)initWithCoder:(NSCoder *)aDecoder
{
self=[super init];
if(self)
{
self.age=[aDecoderdecodeIntForKey:AGE];
self.name=[aDecoderdecodeObjectForKey:NAME];
self.email=[aDecoderdecodeObjectForKey:EMAIL];
self.pwd=[aDecoderdecodeObjectForKey:PASSWORD];
}
return self;
}
@end
第三种:NSUserDefaults sample code:
[[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"]; [[NSUserDefaults standardUserDefaults] synchronize];
第四种、SQlite数据库、CoreData数据库
- iOS 归档的使用
- IOS的归档
- IOS的归档
- iOS 归档的记录
- iOS 归档的记录
- IOS的归档小总结
- ios 归档解归档
- iOS归档、反归档
- iOS归档和解归档
- iOS 归档 反归档
- iOS归档
- iOS 归档
- ios归档
- iOS归档
- ios 归档
- iOS 归档
- iOS 归档
- ios开发中归档的使用
- 常用贴片电阻、电容、电感封装
- nyoj 325-zb的生日
- php语言实现tcpip协议
- PROTEL常用元件封装列表
- rvm的使用笔记
- IOS的归档
- JQuery七:杂记
- 项目进阶 之 持续构建环境搭建(四)Jenkins环境搭建
- 深入探讨电容的种类和作用
- nyoj 456-邮票分你一半
- eclipse 出现Package Explorer出现文件夹为灰的情况
- 看懂论文的机器学习基本知识(三)--假正例、假负例、ROC曲线
- ubuntu12.04安装嵌入式开发环境
- APMServ MySQL 1577错误解决方法