就两个字:去做 码不离手
归档,在其他语言中又叫“序列化”, 就是将对象保存到磁盘;解档,在其他语言中又叫“反序列化”,就是将硬盘文件还原成对象。
在iOS中数据的存储有5中方式:
1.XML属性列表(plist归档)
2.NSUserDefault(偏好设置)
3.NSKeyedArchiver归档(加密形式)
4.SQLite(嵌入式数据库)
5.Core Data(面向对象方式的嵌入式数据库)
1.xml属性列表
优点:操作简单
缺点:明文方式保存,支持NSArray、NSMutableArray、NSDictionary、NSMutableDictionary
方法:wrireToFile、 arrayWithContentsOfFile 、dictionaryWithContentsOfFile (NSString、NSNumber、NSData这几种类型的数据不能使用)
NSString *filePath = @"/Users/Sunjian/Desktop/xml.plist";
NSArray *array = @[@"teachers",@"students"];
[array writeToFile:path atomically:YES];
NSArray *array1 = [NSArray arrayWithContentsOfFile:filePath];
NSDictionary *dic = @{@"name":@"xiaolin",@"age":@"21"};
[dic writeToFile:path atomically:YES];
NSDictionary *dic1 = [NSDictionary dictionaryWithContentsOfFile:file];
2.NSKeyedArchiver归档
//简单归档
NSString *str = @"Hello, world";
NSString *path = @"/Users/Sunjian/Desktop/archiver.arc";
if(![NSArchiver archiveRootObject:str toFile:path]){
NSLog(@"archiver failed");
}
//建档
NSString *str1 = [NSUnarchiver unarchiverObjectWithFile:path];
//NSArray归档
NSString *path2=@"/Users/kenshincui/Desktop/archiver2.arc";
NSArray *array1=@[@"Kenshin",@"Kaoru",@"Rosa"];
if(![NSArchiver archiveRootObject:array1 toFile:path2]){
NSLog(@"archiver failed!");
}
//NSArray解档
NSArray *array2=[NSUnarchiver unarchiveObjectWithFile:path2];
[array2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"array2[%lu]=%@",idx,obj);
}];
//对复杂对象的归档(多对象归档)
NSString *path = @"/Users/Sunjian/Desktop/archiver.arc";
int value = 89;
CGSize size = {12.5, 16.8};
NSNumber *number = @60;
NSString *str = @"Hello, world!";
NSArray *array = @[@"sunjian",@"xiaoli",@"xiaoming"];
NSDictionary *dic = @{@"name":@"xiaoming",@"age":@20};
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyArchiver *archiver = [[NSKeyArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeInt:int forKey:@"value"];
[archiver encodeSize:size1 forKey:@"size"];
[archiver encodeObject:number1 forKey:@"number"];
[archiver encodeObject:str1 forKey:@"string"];
[archiver encodeObject:array1 forKey:@"array"];
[archiver encodeObject:dic1 forKey:@"dic"];
[archiver finishEncoding];
[data writeToFile:path atomically:YES];
//解档
int int2;
CGSize size2;
NSNumber *number2;
NSString *str2;
NSArray *array2;
NSDictionary *dic2;
NSData *data2=[[NSData alloc]initWithContentsOfFile:path1];//读出数据到NSData
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data2];
int2= [unarchiver decodeInt64ForKey:@"int"];
size2=[unarchiver decodeSizeForKey:@"size"];
number2=[unarchiver decodeObjectForKey:@"number"];
str2=[unarchiver decodeObjectForKey:@"string"];
array2=[unarchiver decodeObjectForKey:@"array"];
dic2=[unarchiver decodeObjectForKey:@"dic"];
[unarchiver finishDecoding];
//对自定义的对象进行归档,必须实现NSCoding
-(void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aCoder;
Person.h
@interface Person:NSObject<NSCoding>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, assing) float height;
@property (nonatomic, assign) NSData *birthday;
@end
@implementation Persion
//解码
- (id)initWithCoder:(NSCoder *)aDecoder{
NSLog(@"decode...");
if(self = [super init]){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDocoder decoderInt64ForKey@"age"];
self.height= [aDecoder decodeFloadForKey:@"height"];
self.birthday=[aDecoder decoderObjectForKey:@"birthday"];
}
return self;
}
//编码
- (void)encoderWithCoder:(NSCoder *)aCoder{
NSLog(@"encoder....");
[aCoder encoderObject:_name forKey:@"name"];
[aCoder encoderInt64:_age forKey:@"age"];
[aCoder encoderFloat:_height forKey:@"height"];
[aCoder encoderObject:_birthday forKey:@"birthday"];
}
//重写描述
- (NSString *)description{
}
//使用
int main(int argc, char *argc[]){
Person *person = [[Person alloc] init];
person.name = @"SunJian";
person.age = 28;
person.height = 1.72;
//归档
[NSKeyedArchiver archiveRootObjec:person toFile:path];
//解档
[NSkeyedUnarchiver unarchiveObjectWithFile:path];
}
3.NSUserDefault 全部数据类型
NSUserDefault *userDefault = [NSUserDefault standarUserDefaults];
NSString *str = [userDefault objectForKey:key1];
NSString *dic = [useDefault objectForKey:key2];
[userDefault setObject:str for:key];
[userDefault setObject:dic for:key];
归档
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 利用运行时,归档解归档实现用户信息存储,无需对用户模型的每个属性单独归档与解归档:1、使用方法:保存:UserMo...
- 本篇文章是对于复杂对象来说的 这里我们来实现一个通讯录的功能 这个通讯录一共有三个页面:(1) 主页面 显示名字的...