在開發(fā)中經(jīng)常需要對一些對象進(jìn)行保存偎窘,當(dāng)然這是一些很輕量級(jí)的,我們首先會(huì)想到使用NSUserDefaults進(jìn)行保存溜在,但是NSUserDefaults所能直接保存的對象類型也是有限的陌知,比如NSArray,NSData,NSDictionary,NSNumber,NSString,對于我們自己建立的模型對象,NSUserDefaults直接保存的話掖肋,就有點(diǎn)力不從心了仆葡,這時(shí),我們往往要對模型對象進(jìn)行歸檔志笼,歸檔雖說實(shí)現(xiàn)簡單沿盅,但是,試想一些纫溃,如果你的模型成員比較多腰涧,手動(dòng)實(shí)現(xiàn)很費(fèi)時(shí)間,可以新建一個(gè)NSObject的分類紊浩,使用runtime遍歷屬性實(shí)現(xiàn)歸檔
. h 文件
//
// DVVCoding.h
// DVVCoding <https://github.com/devdawei/DVVCoding.git>
//
// Created by 大威 on 2016/11/21.
// Copyright ? 2016年 devdawei. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol DVVCodingDelegate <NSObject>
@optional
/**
如果有不想緩存的屬性南窗,通過此代理方法返回就可以了
*/
+ (NSArray *)dvv_codingIgnoreProperties;
@end
@interface NSObject (DVVCoding)
/**
* 解碼(從文件解析對象)
*/
- (void)dvv_decode:(NSCoder *)decoder;
/**
* 編碼(將對象寫入文件)
*/
- (void)dvv_encode:(NSCoder *)encoder;
/**
* 屬性歸檔的實(shí)現(xiàn)
*/
#define DVVCodingImplementation \
- (instancetype)initWithCoder:(NSCoder *)aDecoder \
{ \
self = [super init]; \
if (self) { \
[self dvv_decode:aDecoder]; \
} \
return self; \
} \
\
- (void)encodeWithCoder:(NSCoder *)coder \
{ \
[self dvv_encode:coder]; \
}
@end
. m 文件
//
// DVVCoding.m
// DVVCoding <https://github.com/devdawei/DVVCoding.git>
//
// Created by 大威 on 2016/11/21.
// Copyright ? 2016年 devdawei. All rights reserved.
//
#import "DVVCoding.h"
#import <objc/runtime.h>
typedef NS_ENUM(NSUInteger, DVVCodingType)
{
DVVCodingTypeEncode,
DVVCodingTypeDecode,
};
@implementation NSObject (DVVCoding)
- (void)dvv_encode:(NSCoder *)encoder
{
[self dvv_codingFor:encoder type:DVVCodingTypeEncode];
}
- (void)dvv_decode:(NSCoder *)decoder
{
[self dvv_codingFor:decoder type:DVVCodingTypeDecode];
}
- (void)dvv_codingFor:(NSCoder *)coder type:(DVVCodingType)type
{
NSArray *ignoreProperties = nil;
if ([[self class] respondsToSelector:@selector(dvv_codingIgnoreProperties)])
{
ignoreProperties = (NSArray *)[[self class] performSelector:@selector(dvv_codingIgnoreProperties)];
}
if (!ignoreProperties) ignoreProperties = [NSArray array];
// 添加默認(rèn)忽略的屬性
NSMutableArray *defaultIgnoreProperties = [NSMutableArray arrayWithObjects:@"hash", @"superclass", @"description", @"debugDescription", nil];
ignoreProperties = [defaultIgnoreProperties arrayByAddingObjectsFromArray:ignoreProperties];
unsigned int count;
// 獲取屬性列表
objc_property_t *properties = class_copyPropertyList(self.class, &count);
for (unsigned int i = 0; i < count; i++)
{
// 獲取一個(gè)屬性
objc_property_t property = properties[i];
// 獲取一個(gè)屬性名
const char *name = property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:name];
// 過濾屬性
if (ignoreProperties)
{
BOOL flage = NO;
for (NSString *ignorePropertyName in ignoreProperties)
{
if ([propertyName isEqualToString:ignorePropertyName])
{
flage = YES;
break;
}
}
if (flage)
{
continue;
}
}
// 用來存儲(chǔ)一個(gè)屬性值
NSString *propertyValue = nil;
// 編碼
if (DVVCodingTypeEncode == type)
{
propertyValue = [self valueForKey:propertyName];
if(propertyValue) [coder encodeObject:propertyValue forKey:propertyName];
}
// 解碼
else if (DVVCodingTypeDecode == type)
{
propertyValue = [coder decodeObjectForKey:propertyName];
if(propertyValue) [self setValue:propertyValue forKey:propertyName];
}
}
// 釋放
free(properties);
}
@end
直接在模型對象的implementation里調(diào)用宏
#import "MMRDMUserInfo.h"
@implementation MMRDMUserInfo
// 調(diào)用這句宏定義,即可實(shí)現(xiàn)對象歸檔郎楼,不用自己再對每一個(gè)屬性寫繁瑣的編碼和解碼
DVVCodingImplementation
/**
如果有不想緩存的屬性万伤,通過此代理方法返回
@return 忽略列表
*/
+ (NSArray *)dvv_codingIgnoreProperties
{
return @[ @"property_1", @"property_2" ];
}
@end