归档

就两个字:去做 码不离手
归档,在其他语言中又叫“序列化”, 就是将对象保存到磁盘;解档,在其他语言中又叫“反序列化”,就是将硬盘文件还原成对象。
在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

@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];

你可能感兴趣的:(归档)