前言
前几天做项目,需要保存自定义的对象,也就是数据Model,之前也在这篇文章里讲到过存储自定义对象的方法,但是那种方法感觉太麻烦了,需要每次归档model
的每一个属性
,如果需要归档的model
不多,那这种方法还是ok的,如果项目中需要归档的model
比较多的话感觉会很麻烦,所以就想了个工具类来快速实现归档自定义对象
快速归档
- 这个方法是在学习
MJExtension
源码的时候想到的,其实MJExtension
已经实现了这种功能,
- 这个工具类用到的知识点很简单,
runtime
和kvc
而已
这个工具类很简单,目录如下
使用方法
在自定义的Model
的.m文件引入#import "NSObject+WYCoding.h"
文件,然后书写WYCodingImplementation
宏就ok了,如下图所示
这里提供了三个方法来自定义归档的属性
-
+ (NSArray *)wy_allowedCodingPropertyNames;
允许归档的属性数组 -
+ (NSArray *)wy_ignoredCodingPropertyNames;
忽略归档的属性数组 -
+ (BOOL)wy_ignoreCodingSuperClassPropertyNames;
是否忽略归档父类的属性
归档Model
这里自定义了一个Person
类,定义了不同类型的属性如下
@property (nonatomic , copy) NSString *name;
@property (nonatomic , assign) NSInteger age;
@property (nonatomic , assign) long longValue;
@property (nonatomic , assign) double doub;
@property (nonatomic , assign) NSUInteger uint;
@property (nonatomic , strong) NSArray *titles;
@property (nonatomic , strong) Student *stu;
@property (nonatomic , copy) NSDictionary *data;
@property (nonatomic , assign) CGFloat floatNumber;
开始归档Person
- (IBAction)archPerson:(id)sender {
Person *person = [[Person alloc] init];
person.name = @"personName";
person.age = 100;
person.titles = @[@"jack",@"mattt"];
person.data = @{@"list":@[@{@"id":@"187",
@"time":@"2017.4.21"}],
@"success":@"ture"};
person.longValue = 69;
person.doub = 666.66;
person.uint = 90;
person.floatNumber = 10.55;
Student *stu = [[Student alloc] init];
stu.name = @"person的学生";
stu.age = 99;
person.stu = stu;
NSString *filePath = [[WYFileManager getDocumentFilePath] stringByAppendingPathComponent:@"person"];
//保存
[WYFileManager setCustomObject:person forKey:@"person" filePath:filePath];
_currentKey = @"person";
_currentModelButton = sender;
}
解档Person
- (IBAction)unarchPerson:(id)sender {
Person *person = [WYFileManager getCustomObjectWithKey:@"person"];
NSLog(@"person --- name: %@ age: %ld longValue: %ld doub: %ld uint: %lu floatNumber: %.2f student`s properties name: %@ age: %ld ", person.name , (long)person.age, person.longValue, (long)person.doub , (unsigned long)person.uint,person.floatNumber, person.stu.name, person.stu.age);
NSDictionary *data = person.data;
NSLog(@"字典 --- %@", data);
for (NSString *title in person.titles) {
NSLog(@"数组· %@",title);
}
}
打印信息为
2017-05-01 19:25:02.600 WYCoding[10163:2327318] person --- name: personName age: 100 longValue: 69 doub: 666 uint: 90 10.55 student`s properties name: person的学生 age: 99
2017-05-01 19:25:02.600 WYCoding[10163:2327318] 字典 --- {
list = (
{
id = 187;
time = "2017.4.21";
}
);
success = ture;
}
2017-05-01 19:25:02.601 WYCoding[10163:2327318] 数组· jack
2017-05-01 19:25:02.601 WYCoding[10163:2327318] 数组· mattt
- 其中
WYFileManager
文件是我自己写的存储Model
至沙盒文件下的工具类,可以自己实现
最后
因为这个工具是学习 MJExtension
实现的,如果项目中有用到MJExtension
框架的话可以直接使用MJ框架中的方法
MJ的使用
只需要在自定义model
的.m文件中书写MJExtensionCodingImplementation
宏,就可以了
#import "MJExtension.h"
@implementation Person
// 宏实现
MJExtensionCodingImplementation
@end
归档Person
- (IBAction)archiver:(id)sender {
Person *person2 = [[Person alloc] init];
person2.name = @"dev";
person2.age = 28;
person2.titles = @[@"Tom",@"Jerry"];
[WYFileManager setCustomObject:person2 key:@"person2"];
}
解档Person
- (IBAction)unarchiver:(id)sender {
Person *person2 = [WYFileManager getCustomObjectWithKey:@"person2"];
NSLog(@"name %@ age %ld ", person2.name , (long)person2.age);
for (NSString *title in person2.titles) {
NSLog(@"%@",title);
}
}
打印信息为
2017-05-01 19:53:35.206 testMJExtension[10437:2353875] name dev age 28
2017-05-01 19:53:35.206 testMJExtension[10437:2353875] Tom
2017-05-01 19:53:35.207 testMJExtension[10437:2353875] Jerry
代码下载地址:Github下载