iOS利用Runtime实现自定义类的序列化和反序列化

    最近项目中有一个发帖子的需求,就是类似新浪微博中的发文章。现在要求增加草稿箱的功能,不同的帖子都可以将内容存储到本地。开始我打算用数据库做本地存储,但是,由于文本编辑有富文本内容,对应iOS中的NSAttributedString对象,这种对象存储到数据库没有对应的数据类型,然后就采用了做本地序列化和反序列化来存储内容,也就是归解档。

    发帖页面大概是这样的,支持添加标题、封面、文本和图片,文本中支持插入话题和联系人:

  

 

     利用runtime来做序列化,主要是省了自己去一个一个解析类的成员变量和属性了 ,在增加新的属性的时候,可以不用再去关心属性的解析了。注意:如果在子类中实现序列化和反序列化,需要考虑父类属性。

 

     直接贴代码了:

@interface SendPostDraftsBoxModel : NSObject<NSSecureCoding>

@property(nonatomic, copy)NSString *postId;//帖子ID
@property(nonatomic, copy)NSAttributedString *title;//标题
@property(nonatomic, strong)UIImage *coverImage;//封面
@property(nonatomic, strong)NSArray *contentArray;//拼装的内容,存的是字典,字典里边存的NSAttributeString和UIImage

//序列化到本地
+ (BOOL)writeDraftsBoxModelWithModel:(SendPostDraftsBoxModel *)model;

//反序列化,返回model对象
+ (SendPostDraftsBoxModel *)readDraftsBoxModelWithPostId:(nonnull NSString *)postId;

//删除本地的草稿箱model对象
+ (BOOL)deleteDraftsBoxModelWithPostId:(nonnull NSString *)postId;

@end
@implementation SendPostDraftsBoxModel

+ (BOOL)supportsSecureCoding {
    return YES; //支持加密编码
}

- (void)encodeWithCoder:(NSCoder *)coder {
    
    unsigned int count;
    Ivar *varArray = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++) {
        Ivar var = varArray[i];
        const char *cName = ivar_getName(var);
        NSString *proName = [NSString stringWithUTF8String:cName];
        //去掉成员变量前边的"_"
        if (proName.length > 0 && [[proName substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"_"]) {
            proName = [proName substringFromIndex:1];
        }
        
        id value = [self valueForKey:proName];
        [coder encodeObject:value forKey:proName];
    }
    free(varArray);
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        unsigned int count;
        Ivar *varArray = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i++) {
            Ivar var = varArray[i];
            const char *cName = ivar_getName(var);
            NSString *proName = [NSString stringWithUTF8String:cName];
            //去掉成员变量前边的"_"
            if (proName.length > 0 && [[proName substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"_"]) {
                proName = [proName substringFromIndex:1];
            }
            id value = [coder decodeObjectForKey:proName];
            if (value != nil) {
                [self setValue:value forKey:proName];
            }
        }
        free(varArray);
    }
    
    return self;
}

+ (BOOL)writeDraftsBoxModelWithModel:(SendPostDraftsBoxModel *)model {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [NSString stringWithFormat:@"/SendPostDraftsBoxModel_%@.achive", model.postId ? model.postId : @""];
    NSString *docPath = [path stringByAppendingString:fileName];
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
    if (!isExist) {
        [[NSFileManager defaultManager] createFileAtPath:docPath contents:nil attributes:nil];
    }
    return [NSKeyedArchiver archiveRootObject:model toFile:docPath];
}

+ (SendPostDraftsBoxModel *)readDraftsBoxModelWithPostId:(nonnull NSString *)postId {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [NSString stringWithFormat:@"/SendPostDraftsBoxModel_%@.achive", postId ? postId : @""];
    NSString *docPath = [path stringByAppendingString:fileName];
    SendPostDraftsBoxModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    return model;
}

+ (BOOL)deleteDraftsBoxModelWithPostId:(nonnull NSString *)postId {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [NSString stringWithFormat:@"/SendPostDraftsBoxModel_%@.achive", postId ? postId : @""];
    NSString *docPath = [path stringByAppendingString:fileName];
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
    if (isExist) {
       return [[NSFileManager defaultManager] removeItemAtPath:docPath error:nil];
    }
    return YES;
}
@end

 

   这里有一个考虑父类的情况,并且进行了封装,大家也可以参考:https://github.com/weng1250/WZLSerializeKit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值