美文网首页
iOS利用runtime运行时自动归档、解档

iOS利用runtime运行时自动归档、解档

作者: 一只大码农 | 来源:发表于2019-02-25 10:54 被阅读0次

废话不多说,直接上代码

归档

- (void)sea_encodeWithCoder:(NSCoder *)coder
{
    [self sea_encodeWithCoder:coder clazz:[self class]];
}

- (void)sea_encodeWithCoder:(NSCoder*) coder clazz:(Class) clazz
{
    if(clazz == [NSObject class]){
        return;
    }
    
    //获取当前类的所有属性,该方法无法获取父类或者子类的属性
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList(clazz, &count);
    for(int i = 0;i < count;i ++){
        
        objc_property_t property = properties[i];
        NSString *name = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        
        //类型地址 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW6
        NSString *attr = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
        
        NSArray *attrs = [attr componentsSeparatedByString:@","];
        
        //判断是否是只读属性
        if(attrs.count > 0 && ![attrs containsObject:@"R"]){
            
            id value = [self valueForKey:name];
            [coder encodeObject:value forKey:name];
        }
    }
    
    //递归获取父类的属性
    [self sea_encodeWithCoder:coder clazz:[clazz superclass]];
}

解档

- (void)sea_initWithCoder:(NSCoder *)decoder
{
    [self sea_initWithCoder:decoder clazz:[self class]];
}

- (void)sea_initWithCoder:(NSCoder*) decoder clazz:(Class) clazz
{
    if(clazz == [NSObject class]){
        return;
    }
    
    //获取当前类的所有属性,该方法无法获取父类或者子类的属性
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList(clazz, &count);
    for(int i = 0;i < count;i ++){
        
        objc_property_t property = properties[i];
        NSString *name = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        
        //类型地址 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW6
        NSString *attr = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
        
        NSArray *attrs = [attr componentsSeparatedByString:@","];
        
        //判断是否是只读属性
        if(attrs.count > 0 && ![attrs containsObject:@"R"]){
            
            NSString *type = [attrs firstObject];
            id value = nil;
            //判断是否是对象属性
            if([type containsString:@"T@\""]){
                type = [type stringByReplacingOccurrencesOfString:@"T@\"" withString:@""];
                type = [type stringByReplacingOccurrencesOfString:@"\"" withString:@""];
                
                value = [decoder decodeObjectOfClass:NSClassFromString(type) forKey:name];
                
            }else{
                value = [decoder decodeObjectForKey:name];
            }
            
            [self setValue:value forKey:name];
        }
    }
    
    //递归获取父类的属性
    [self sea_initWithCoder:decoder clazz:[clazz superclass]];
}

相关文章

  • iOS利用runtime运行时自动归档、解档

    废话不多说,直接上代码 归档 解档

  • IOSRunTime_OC的序列化

    利用RunTime运行时,遍历一个类的所有属性和方法。 序列化-----归档 反序列化--解档 在iOS中一个自定...

  • runtime使用 自定义数据类型的编码解码

    通过runtime 遍历自定义model的所有属性实现归档解档操作。 要实现我们自定义的model能够自动解档归档...

  • runtime自动归档

    前言 善用runtime,可以解决自动归档解档。想想以前归档是手动写的,确实太麻烦了。现在有了runtime,我们...

  • runtime自动归档/解档

    前言 善用runtime,可以解决自动归档解档。想想以前归档是手动写的,确实太麻烦了。现在有了runtime,我们...

  • runTime

    iOS runtime讲解,并且用runtime动态归档与解档 (2015-09-30 22:42:02)转载▼ ...

  • 利用runtime归档-解档

    利用runtime归档的好处就在于,不管类的属性有多少个,都不用担心,它都会自动帮你处理好。 首先新建一个Pers...

  • runtime自动解档归档

    如果你实现过自定义模型数据持久化的过程,那么你也肯定明白,如果一个模型有许多个属性,那么我们需要对每个属性都实现一...

  • OC 利用 Runtime 自动实现 归档 解档

    在 基类class 中实现如下代码: runtime 机制学习参考链接:https://www.jianshu.c...

  • iOS Runtime归档解档

    利用运行时实现归档、解档,并将其封装成宏 继承 NSObject 并遵守 NSCoding 协议,创建一个类 在 ...

网友评论

      本文标题:iOS利用runtime运行时自动归档、解档

      本文链接:https://www.haomeiwen.com/subject/fwewyqtx.html