iOS运行时的使用-归档反归档详解

2020-03-08  本文已影响0人  MiniCoder

在开发中有时会用到对象的归档和反归档,属性比较少的时候还比较好,但是如果属性过多的话,写起来会比较麻烦,通过运行时定的方式,我们可以遍历对象的属性然后进行赋值,而且不同的模型可以通用。

///归档
- (void)encodeWithCoder:(nonnull NSCoder *)coder {
    unsigned int count = 0;
    Ivar * ivars = class_copyIvarList([Person class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        const char * name = ivar_getName(ivar);
        NSString * key = [[NSString alloc]initWithUTF8String:name];
        [coder encodeObject:[self valueForKey:key] forKey:key];
    }
    free(ivars);
}
///反归档
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
    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];
              NSString * key = [[NSString alloc]initWithUTF8String:ivar_getName(ivar)];
              id value =  [coder decodeObjectForKey:key];
              [self setValue:value forKey:key];
          }
          free(ivars);
    }
    return  self;
}

使用运行时需要注意释放copy的属性列表。

///归档
+ (nullable NSData *)archivedDataWithRootObject:(id)object requiringSecureCoding:(BOOL)requiresSecureCoding error:(NSError **)error
///反归档
+ (nullable id)unarchivedObjectOfClass:(Class)cls fromData:(NSData *)data error:(NSError **)error

如果归档时requiringSecureCoding参数传的是true的话,那么需要在模型中实现该方法。

+(BOOL)supportsSecureCoding{
    return true;
}

下面是调用代码:

    Person * person = [[Person alloc]init];
    person.name = @"张三";
    NSString * path  = [NSTemporaryDirectory() stringByAppendingPathComponent:@"text"];
    NSError * error;
    NSData * objData =  [NSKeyedArchiver archivedDataWithRootObject:person requiringSecureCoding:true error:&error];
    if (objData != nil) {
        [objData writeToFile:path atomically:false];
    }
    NSError * unarchiveError;
    Person * newPerson =  [NSKeyedUnarchiver unarchivedObjectOfClass:[Person class] fromData:[NSData dataWithContentsOfFile:path] error:&unarchiveError];
    NSLog(@"%@ %@",path,newPerson.name);
截屏2020-03-0814.47.19.png

我们可以看到信息已经可以打印出来了。
如果对你有所帮助的话,希望点个关注。

上一篇下一篇

猜你喜欢

热点阅读