ios中本地化存储的方式之一,个人觉得跟PList的方式类似。只是我们不需要加载从Plist获得字典后再人为去做一遍字典转模型
storyboard中加上两个按钮:归档,修改和接档
mark --- 需要实现归档接单的类
//
// Person.h
// test
//
// Created by lh0811 on 16/7/17.
// Copyright © 2016年 lh0811. All rights reserved.
//
#import <Foundation/Foundation.h>
//要让一个类实现归档接档,只需要醉寻NSCoding协议,并实现协议方法即可
@interface Person : NSObject<NSCoding>
@property (nonatomic,copy) NSString * name;
@property (nonatomic,assign) NSInteger age;
@end
//
// Person.m
// test
//
// Created by lh0811 on 16/7/17.
// Copyright © 2016年 lh0811. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
mark --- 我们看看NSCoding里面都有神马
@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
@end
好吧 除了这两个方法也没啥了。。。
mark --- VC 中实现归档 解档
//
// ViewController.m
// test
//
// Created by lh0811 on 16/7/17.
// Copyright © 2016年 lh0811. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
//点击归档
- (IBAction)clickGuiDang:(id)sender {
Person * p = [[Person alloc]init];
p.name = @"张三";
p.age = 12;
NSString * home = NSHomeDirectory();
NSString * fileStr = [home stringByAppendingPathComponent:@"p.archive"];
[NSKeyedArchiver archiveRootObject:p toFile:fileStr];
NSLog(@"归档:%@",fileStr);
}
//点击修改
- (IBAction)clickXiuGai:(id)sender {
NSString * home = NSHomeDirectory();
NSString * fileStr = [home stringByAppendingPathComponent:@"p.archive"];
id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:fileStr];
NSLog(@"%@",obj);
Person * p = (Person *)obj;
p.name = @"李四";
p.age = 16;
[NSKeyedArchiver archiveRootObject:p toFile:fileStr];
NSLog(@"归档:%@",fileStr);
}
//点击接档
- (IBAction)clickJieDang:(id)sender {
NSString * home = NSHomeDirectory();
NSString * fileStr = [home stringByAppendingPathComponent:@"p.archive"];
id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:fileStr];
NSLog(@"%@",obj);
Person * p = (Person *)obj;
NSLog(@"%@",p.name);
NSLog(@"%ld",p.age);
}
@end
mark --- 点击归档 再点击解档 输出结果
2016-07-17 11:11:19.369 test[1948:80172] <Person: 0x7ff5f0569550>
2016-07-17 11:11:19.369 test[1948:80172] 张三
2016-07-17 11:11:19.369 test[1948:80172] 12
mark --- 点击归档->解档 ->修改 ->解档 输出结果
2016-07-17 11:17:25.688 test[1985:86318] 归档:/Users/lh0811/Library/Developer/CoreSimulator/Devices/FB15308A-1108-4C64-8AF1-5B69CF31ABB4/data/Containers/Data/Application/E369A323-FB72-4B19-B2A6-C288F60507D3/p.archive
2016-07-17 11:17:50.358 test[1985:86318] 归档:/Users/lh0811/Library/Developer/CoreSimulator/Devices/FB15308A-1108-4C64-8AF1-5B69CF31ABB4/data/Containers/Data/Application/E369A323-FB72-4B19-B2A6-C288F60507D3/p.archive
2016-07-17 11:17:52.100 test[1985:86318] <Person: 0x7fb0c1e0c960>
2016-07-17 11:17:52.100 test[1985:86318] 张三
2016-07-17 11:17:52.100 test[1985:86318] 12
2016-07-17 11:17:53.081 test[1985:86318] <Person: 0x7fb0c1e0d910>
2016-07-17 11:17:53.082 test[1985:86318] 归档:/Users/lh0811/Library/Developer/CoreSimulator/Devices/FB15308A-1108-4C64-8AF1-5B69CF31ABB4/data/Containers/Data/Application/E369A323-FB72-4B19-B2A6-C288F60507D3/p.archive
2016-07-17 11:17:54.134 test[1985:86318] <Person: 0x7fb0c1e02800>
2016-07-17 11:17:54.134 test[1985:86318] 李四
2016-07-17 11:17:54.134 test[1985:86318] 16