zoukankan      html  css  js  c++  java
  • iOS解档与归档

    // 解档

    - (instancetype)initWithCoder:(NSCoder *)aDecoder

    {

        if (self = [super init]) {

            unsigned int count;

            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* strName = [NSString stringWithUTF8String:name];

                id value = [aDecoder decodeObjectForKey:strName];

                [self setValue:value forKey:strName];

            }

            free(ivars);

        }

        return self;

    }

    // 归档

    - (void)encodeWithCoder:(NSCoder *)aCoder

    {

        unsigned int count;

        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* strName = [NSString stringWithUTF8String:name];

            id value = [self valueForKey:strName];

            [aCoder encodeObject:value forKey:strName];

        }

        free(ivars);

    }

    头文件必须导入 

    #import <objc/runtime.h>

    1
  • 相关阅读:
    自己搭建二维码接口
    HTML CSS SPRITE 工具
    Codeforces Round #636 (Div. 3) 题解
    Codeforces Round #612 (Div. 1+Div. 2)
    计树问题小结 version 2.0
    Educational Codeforces Round 85 (Rated for Div. 2) 题解
    luogu6078 [CEOI2004]糖果
    luogu [JSOI2012]分零食
    多项式全家桶
    生成函数小结
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/5557852.html
Copyright © 2011-2022 走看看