IOS 利用运行时机制来进行归档解档

前言:我们需要对一个类的属性进行归档解档的时候属性特别的多,这时候,我们就会写很多对应的代码,但是如果使用了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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值