Skip to content

Latest commit

 

History

History
48 lines (42 loc) · 1.44 KB

如何运用 Runtime 进行模型的归解档.md

File metadata and controls

48 lines (42 loc) · 1.44 KB

如何运用 Runtime 进行模型的归解档

- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count = 0;
    //  利用runtime获取实例变量的列表
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i ++) {
        //  取出i位置对应的实例变量
        Ivar ivar = ivars[i];
        //  查看实例变量的名字
        const char *name = ivar_getName(ivar);
        //  C语言字符串转化为NSString
        NSString *nameStr = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
        //  利用KVC取出属性对应的值
        id value = [self valueForKey:nameStr];
        //  归档
        [encoder encodeObject:value forKey:nameStr];
    }
    //  记住C语言中copy出来的要进行释放
    free(ivars);
 
}
 
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        unsigned int count = 0;
        Ivar *ivars = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i ++) {
            Ivar ivar = ivars[i];
            const char *name = ivar_getName(ivar);
 
            //
            NSString *key = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
            id value = [decoder decodeObjectForKey:key];
            //  设置到成员变量身上
            if (!value) continue;
            [self setValue:value forKey:key];
        }
        free(ivars);
    }
    return self;
}