归档解档是OC 中很重要的知识点
弥补了NSUserDefaults 的不足 可以存储自定义的对象
我们可以存储多个对象 需要一个数组接受即可
#import <Foundation/Foundation.h>
@interface User : NSObject<NSCoding>
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *passwd;
+ (User *)userWithName:(NSString *)name password:(NSString *)
password;
@end
#import "User.h"
@implementation User
+ (User *)userWithName:(NSString *)name password:(NSString *)password
{
User *user = [[User alloc]init];
user.name = name;
user.passwd = password;
return user;
}
//归档调用方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.passwd forKey:@"passwd"];
}
//接档调用方法
//取值
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.passwd = [aDecoder decodeObjectForKey:@"passwd"];
}
return self;
}
@end
#import "ViewController.h"
#import "User.h"
#import "UserDefaultKeysConfig.h"
@interface ViewController ()
#pragma mark - 属性方法定义
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwdTextField;
- (IBAction)loginBtn:(id)sender;
- (IBAction)exitBtn:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.usernameTextField.text = @"angel";
// self.passwdTextField.text = @"123456";
//
// self.usernameTextField.text = [[NSUserDefaults standardUserDefaults]objectForKey:kUserNameKey];
// self.passwdTextField.text = [[NSUserDefaults standardUserDefaults]objectForKey:kUserPwdKey];
// Do any additional setup after loading the view, typically from a nib.
User *user = [self userFromArachiver];
self.usernameTextField.text = user.name;
self.passwdTextField.text = user.passwd;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 存储信息
//点击登陆按钮 将信息存储到沙盒
- (IBAction)loginBtn:(id)sender {
// [[NSUserDefaults standardUserDefaults]setObject:self.usernameTextField.text forKey:kUserNameKey];
// [[NSUserDefaults standardUserDefaults]setObject:self.passwdTextField.text forKey:kUserPwdKey];
//
// //同步写入文件
// [[NSUserDefaults standardUserDefaults]synchronize];
//1.封装数据模型 用提供的类方法快速创建一个类
User *user = [User userWithName:self.usernameTextField.text password:self.passwdTextField.text];
[self saveUserObjectWithUser:user];
}
#pragma mark - 解归档
- (User *)userFromArachiver
{
//1.首先判断文件是否存在
if(![[NSFileManager defaultManager] fileExistsAtPath:[[self class]path]])
{
return nil;
}
//2.读取data对象
NSData *data = [NSData dataWithContentsOfFile:[[self class]path]];
//3.穿件解归档对象
NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//4.解档 创建接受对象
User * user = [unarchive decodeObjectForKey:@"user"];
//5.完成解档
[unarchive finishDecoding];
NSLog(@"%@",user.name);
NSLog(@"%@---",user.passwd);
return user;
}
#pragma mark - 归档
/**
* 归档
*
* @param user <#user description#>
*/
- (void)saveUserObjectWithUser:(User *)user
{
//我们要将自定义对象转化为二进制流 并写入沙盒 我们要进行以下操作
//1.先创建一个NSMutableData对象
NSMutableData *data = [NSMutableData data];
//2.创建一个归档对象
NSKeyedArchiver *archive = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//3.归档
[archive encodeObject:user forKey:@"user"];
//4.完成归档
[archive finishEncoding];
//5.写入文件
[data writeToFile: [[self class] path] atomically:YES];
}
//怎样调用一个类方法
+ (NSString *)path
{
NSString *chachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask , YES)lastObject];
return [chachePath stringByAppendingPathComponent:@"user"];
}
#pragma mark - 退出
- (IBAction)exitBtn:(id)sender {
[[NSUserDefaults standardUserDefaults]removeObjectForKey:kUserNameKey];
[[NSUserDefaults standardUserDefaults]removeObjectForKey:kUserPwdKey];
[[NSUserDefaults standardUserDefaults]synchronize];
self.usernameTextField.text = @"";
self.passwdTextField.text = @"";
}
@end