自定义类的归档解挡操作
1.我们自己创建一个Student类,它里面有三个属性,并遵循了<NSCoding>协议
#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCoding>
@property (nonatomic ,strong) NSString *name;
@property (nonatomic ,assign) int age;
@property (nonatomic ,strong) NSString *adder;
@end
#import "Student.h"
@implementation Student
-(NSString *)description{
return [NSString stringWithFormat:@"name is: %@, age is: %i,addr is: %@",self.name,self.age,self.adder];
}
//+ (BOOL)supportsSecureCoding{
// return YES;
//}
//实现解挡方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.adder = [aDecoder decodeObjectForKey:@"adder"];
}
return self;
}
//实现归档方法
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeObject:self.adder forKey:@"adder"];
}
@end
在ViewController.h定义两个按钮来触发归档解挡
#import <UIKit/UIKit.h>
#import "Student.h"
@interface ViewController : UIViewController
@property (nonatomic ,strong) UIButton *btn;
@property (nonatomic ,strong) UIButton *btn1;
@end
#import "ViewController.h"
#define PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"student.liu"]
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
[self.btn setTitle:@"DATA归档" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(dataStudenst) forControlEvents:UIControlEventTouchUpInside];
self.btn.backgroundColor = [UIColor redColor];
[self.view addSubview:self.btn];
self.btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 150, 200, 40)];
[self.btn1 setTitle:@"DATA解归档" forState:UIControlStateNormal];
[self.btn1 addTarget:self action:@selector(jiedata) forControlEvents:UIControlEventTouchUpInside];
self.btn1.backgroundColor = [UIColor redColor];
[self.view addSubview:self.btn1];
NSLog(@"%@",PATH);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//归档
-(void)dataStudenst{
Student *stu = [[Student alloc] init];
stu.name = @"liumu";
stu.age = 22;
stu.adder = @"guiyangxueyuan";
// NSMutableData *data = [NSMutableData data];
// NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
BOOL bol = [NSKeyedArchiver archiveRootObject:stu toFile:PATH];
// [archive encodeObject:stu forKey:@"stu"];
// [archive finishEncoding];
// // NSLog(@"%@",data);
//
// BOOL bol = [data writeToFile:PATH atomically:YES];
NSLog(@"%i",bol);
}
//解挡
-(void)jiedata{
// NSData *data = [NSData dataWithContentsOfFile:PATH];
// NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
// Student *stu = [unarchive decodeObjectForKey:@"stu"];
Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
NSLog(@"%@",stu);
}
@end