前言:我们需要对一个类的属性进行归档解档的时候属性特别的多,这时候,我们就会写很多对应的代码,但是如果使用了runtime就可以动态设置,就会变的很轻松!
首先需要导入<objc/runtime.h>
其次归档解档需要遵循NSCoding协议
核心代码
- (void)encodeWithCoder:(NSCoder *)encoder
{
unsigned int count = 0;
//1.获取该类所有的成员变量
Ivar *ivars = class_copyIvarList([IWMerchantBasicInfo class], &count);
for (int i = 0; i < count; i ++) {
Ivar ivar = ivars[i];
//2.查看成员变量
const char *name = ivar_getName(ivar);
//3.根据key获取到对应的value
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
//4.归档
[encoder encodeObject:value forKey:key];
}
//5.释放
free(ivars);
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
unsigned int count = 0;
//1.获取该类所有的成员变量
Ivar *ivars = class_copyIvarList([IWMerchantBasicInfo class], &count);
for (int i = 0 ; i < count; i ++) {
Ivar ivar = ivars[i];
//2.查看成员变量
const char *name = ivar_getName(ivar);
//3.根据key获取到对应的value
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[self setValue:value forKey:key];
}
//4.释放
free(ivars);
}
return self;
}
示例代码:
.h文件
#import <Foundation/Foundation.h>
@interface IWMerchantBasicInfo : NSObject<NSCoding>
//商铺编码
@property (copy,nonatomic) NSString *merchant_id;
//商铺名称
@property (copy,nonatomic) NSString *merchant_name;
//商铺Logo
@property (copy,nonatomic) NSString *logo;
//商铺描述
@property (copy,nonatomic) NSString *merchant_des;
//商铺描述图片
@property (copy,nonatomic) NSString *merchant_des_pic;
//商铺描述图片
@property (copy,nonatomic) NSArray *merchant_des_pics;
//联系人
@property (copy,nonatomic) NSString *contact;
//联系手机号码
@property (copy,nonatomic) NSString *contact_phone;
//固定电话号码
@property (copy,nonatomic) NSString *landline_phone;
//营业时间
@property (copy,nonatomic) NSString *business_times;
//广告语
@property (copy,nonatomic) NSString *ad;
//商家分类
@property (copy,nonatomic) NSString *category_id;
//经度
@property (assign,nonatomic) float longitude;
//纬度
@property (assign,nonatomic) float latitude;
//店铺地址
@property (copy,nonatomic) NSString *merchant_address;
//商家活动小图标
@property (copy,nonatomic) NSString *bd_thumbnail;
//商家活动小图标数组
@property (nonatomic,strong) NSArray *bdThumbnails;
//评分
@property (assign,nonatomic) float score;
//是否被收藏
@property (nonatomic,assign) BOOL collected;
@end
.m文件
#import "IWMerchantBasicInfo.h"
#import <objc/runtime.h>
@implementation IWMerchantBasicInfo
- (void)setBd_thumbnail:(NSString *)bd_thumbnail
{
_bd_thumbnail = bd_thumbnail;
if (_bd_thumbnail.length != 0) {
NSMutableArray *array = (NSMutableArray *)[_bd_thumbnail componentsSeparatedByString:@","];
for (NSString *arrayStr in array) {
if (arrayStr.length == 0 || [arrayStr isEqualToString:@""]) {
[array removeObject:arrayStr];
}
}
_bdThumbnails = array;
}
}
- (void)setMerchant_des_pic:(NSString *)merchant_des_pic
{
_merchant_des_pic = merchant_des_pic;
if (_merchant_des_pic.length != 0) {
NSMutableArray *array = (NSMutableArray *)[_merchant_des_pic componentsSeparatedByString:@","];
for (NSString *arrayStr in array) {
if (arrayStr.length == 0 || [arrayStr isEqualToString:@""]) {
[array removeObject:arrayStr];
}
}
_merchant_des_pics = array;
}
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
unsigned int count = 0;
//1.获取该类所有的成员变量
Ivar *ivars = class_copyIvarList([IWMerchantBasicInfo class], &count);
for (int i = 0; i < count; i ++) {
Ivar ivar = ivars[i];
//2.查看成员变量
const char *name = ivar_getName(ivar);
//3.根据key获取到对应的value
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
//4.归档
[encoder encodeObject:value forKey:key];
}
//5.释放
free(ivars);
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
unsigned int count = 0;
//1.获取该类所有的成员变量
Ivar *ivars = class_copyIvarList([IWMerchantBasicInfo class], &count);
for (int i = 0 ; i < count; i ++) {
Ivar ivar = ivars[i];
//2.查看成员变量
const char *name = ivar_getName(ivar);
//3.根据key获取到对应的value
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[self setValue:value forKey:key];
}
//4.释放
free(ivars);
}
return self;
}
@end