iOS序列化-归档

Posted Eric博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS序列化-归档相关的知识,希望对你有一定的参考价值。

实现<NSCoding>协议


#import "HMCThread.h"
#import <objc/message.h>

@implementation HMCThread

- (void)dealloc
    NSLog(@"HMCThread---dealloc");


- (void)encodeWithCoder:(NSCoder *)aCoder
    unsigned int outCount = 0;
    Ivar *vars = class_copyIvarList([self class], &outCount);
    for(int i=0; i<outCount; i++)
        Ivar var = vars[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(var)];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    


- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
    if(self = [super init])
        unsigned int outCount = 0;
        Ivar *vars = class_copyIvarList([self class], &outCount);
        for(int i=0; i<outCount; i++)
            Ivar var = vars[i];
            NSString *key = [NSString stringWithUTF8String:ivar_getName(var)];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        
    
    
    return self;

以上是关于iOS序列化-归档的主要内容,如果未能解决你的问题,请参考以下文章

iOS序列化-归档

iOS序列化-归档

序列化与归档?

iOS数据持久化存储之归档NSKeyedArchiver

iOS 对模型对象进行归档

IOS数据持久化之归档NSKeyedArchiver