很多情况下,我们都是使用数据库存储数据,但是牵扯到各个model类关联的数据存储时,数据库就有点耗时耗力了,下面我们介绍下序列化存储
1,首先序列化存储的model类需要实现NSCoding协议
#import <Foundation/Foundation.h>
@interface StudentModel : NSObject<NSCoding>
@property (nonatomic,copy) NSString * stu_name;
@property (nonatomic,strong) NSMutableArray * student_likes;
-(id)initWithDic:(NSDictionary *)dic;
#import "StudentModel.h"
#import "LikeModel.h"
@implementation StudentModel
-(id)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self)
{
self.stu_name = dic[@"stu_name"];
for (NSDictionary * likeDic in dic[@"student_likes"])
{
LikeModel * likeModel = [[LikeModel alloc]initWithDic:likeDic];
[self.student_likes addObject:likeModel];
}
}
return self;
}
-(NSMutableArray *)student_likes
{
if (_student_likes == nil)
{
_student_likes = [NSMutableArray array];
}
return _student_likes;
}
//对象编码的时候 ,对象的属性会调用这个方法
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_stu_name forKey:@"stu_name"];
[aCoder encodeObject:_student_likes forKey:@"student_likes"];
}
//对象解码的时候 会调用这个方法
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
_stu_name = [aDecoder decodeObjectForKey:@"stu_name"];
_student_likes = [aDecoder decodeObjectForKey:@"student_likes"];
}
return self;
}
@end
2,然后就是编码存储数据,解码获取数据
#import "ViewController.h"
#import "JSONKit.h"
#import "StudentModel.h"
#import "LikeModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
NSArray * strArray = @[@"编码",@"解码"];
for (int i = 0; i<2; i++)
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(100, 100+150*i, 100, 100)];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTag:i];
[btn setTitle:strArray[i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickToBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
-(void)clickToBtn:(UIButton *)sender
{
if (sender.tag == 0)
{
//获取model(模拟数据)
NSMutableArray * aa = [NSMutableArray arrayWithObjects:@{@"color":@"222"}, nil];
NSArray * array = @[@{@"stu_name":@"111",@"student_likes":aa}];
NSData * jsonData = [array JSONData];
NSArray * studentArr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
StudentModel * studentModel = [[StudentModel alloc]initWithDic:studentArr[0]];
//编码
//实例化一个可变的data
NSMutableData * data = [[NSMutableData alloc] init];
//初始化编码类 并且设置data
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:studentModel];
[archiver finishEncoding];
BOOL isEncode = [data writeToFile:[self getFilePath:@"123.plist"] atomically:YES];
NSLog(@"写入%@",isEncode == YES?@"成功":@"失败");
}
else
{
//解码
NSData * data = [NSData dataWithContentsOfFile:[self getFilePath:@"123.plist"]];
NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
StudentModel * studentModel = [unArchiver decodeObject];
LikeModel * model = studentModel.student_likes[0];
[unArchiver finishDecoding];
}
}
-(NSString *)getFilePath:(NSString *)fileName
{
NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName];
NSLog(@"filePath =====%@",filePath);
return filePath;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end