首页 > 代码库 > 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";}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。