首页 > 代码库 > NSKeyedArchiver的使用

NSKeyedArchiver的使用

 

NSKeyedArchiver是加密形式的保存数据。

 

上代码。

 

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        self.title=@"NSKeyedArchiver";        [self write];    [self read];    }-(void)write{    NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:@"李华",@"name",@"",@"sex",@"20",@"age",nil];    NSLog(@"----write---dataDic---%@",dataDic);        NSData *archiveData =http://www.mamicode.com/ [NSKeyedArchiver archivedDataWithRootObject:dataDic];    NSUserDefaults *archiveDefaults=[NSUserDefaults standardUserDefaults];    [archiveDefaults setObject:archiveData forKey:@"ARCHIVE"];    [archiveDefaults synchronize];}-(void)read{    NSUserDefaults *readDefault=[NSUserDefaults standardUserDefaults];    NSData *readDic=[readDefault objectForKey:@"ARCHIVE"];        NSDictionary  *dataDic = [[NSDictionary alloc]initWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:readDic]];        NSLog(@"---read---dataDic---%@",dataDic);    }

 

打印出的结果:

 

2014-06-26 09:38:51.375 NSKeyedArchiver的使用[1153:60b] ----write---dataDic---{    age = 20;    name = "\U674e\U534e";    sex = "\U5973";}2014-06-26 09:38:51.376 NSKeyedArchiver的使用[1153:60b] ---read---dataDic---{    age = 20;    name = "\U674e\U534e";    sex = "\U5973";}