归档
来源:互联网 发布:郎佐-鲍尔 数据 nba 编辑:程序博客网 时间:2024/04/28 05:15
归档也叫序列化,是将文件存在硬盘,解档是从硬盘还原
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数据库
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- 归档
- Android之百度地图GPS定位
- 关于ListView 内子控件监听
- js 闭包改变值
- 转换汉字为unicode形式的字符串和转换unicode形式字符串转换成汉字
- java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
- 归档
- opencv之运动模板跟踪
- memset()函数
- Android volley添加Cookie
- 总线设备驱动框架程序 及 struct device中消失的bus_id的取代方法
- iOS 截取屏幕和获取图片的一部分
- XPS打不开mph或ucf文件的解决办法
- POJ2105 IP Address
- 严重: Null component Catalina:type=JspMonitor,name=jsp,WebModule=//localhost/sshnews,J2EEApplicatio