【开发技巧】KVC的利用:1,实现归档中的 encodeWithCoder 和 initWithCoder方法

1,归档和解档

- (void)encodeWithCoder:(NSCoder *)aCoder {
    unsigned int count;
    Ivar *ivar = class_copyIvarList([self class], &count);
    for (int i = 0 ; i < count ; i++) {
        Ivar iv = ivar[i];
        const char *name = ivar_getName(iv);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        [aCoder encodeObject:value forKey:strName];
    }
    free(ivar);
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self != nil) {
        unsigned int count = 0;
        Ivar *ivar = class_copyIvarList([self class], &count);
        for (int i= 0 ;i < count ; i++) {
            Ivar var = ivar[i];
            const char *keyName = ivar_getName(var);
            NSString *key = [NSString stringWithUTF8String:keyName];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(ivar);
    }
    return self;
}

对比普通方法

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeBool:self.launchTrendsStatus forKey:@"launchTrendsStatus"];
    [aCoder encodeInteger:self.trendsModelType forKey:@"trendsModelType"];
    [aCoder encodeInt:self.ID forKey:@"ID"];

    [aCoder encodeObject:self.coverImage forKey:@"coverImage"];
    [aCoder encodeObject:self.voiceFileUrl forKey:@"voiceFileUrl"];
    [aCoder encodeObject:self.imageIdentitiyArr forKey:@"imageIdentitiyArr"];
    [aCoder encodeObject:self.urlArr forKey:@"urlArr"];
    
    // 模型
    [aCoder encodeObject:self.trendsModel forKey:@"trendsModel"];
    [aCoder encodeObject:self.voiceModel forKey:@"voiceModel"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        self.launchTrendsStatus = [aDecoder decodeBoolForKey:@"launchTrendsStatus"];
        self.trendsModelType = [aDecoder decodeIntegerForKey:@"trendsModelType"];
        self.ID = [aDecoder decodeIntForKey:@"ID"];
        self.coverImage = [aDecoder decodeObjectForKey:@"coverImage"];
        self.voiceFileUrl = [aDecoder decodeObjectForKey:@"voiceFileUrl"];
        self.imageIdentitiyArr = [aDecoder decodeObjectForKey:@"imageIdentitiyArr"];
        self.urlArr = [aDecoder decodeObjectForKey:@"urlArr"];
        
        // 模型
        self.voiceModel = [aDecoder decodeObjectForKey:@"voiceModel"];
        self.trendsModel = [aDecoder decodeObjectForKey:@"trendsModel"];
    }
    return self;
}

2,打印自定义对象信息

- (NSString *)description{
    
    NSMutableString *desc = @"对象{\n".mutableCopy;
    
    unsigned int count;
    Ivar *ivar = class_copyIvarList([self class], &count);
    for (int i = 0 ; i < count ; i++) {
        Ivar iv = ivar[i];
        const char *name = ivar_getName(iv);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        [desc appendString:[NSString stringWithFormat:@", %@: %@\n", strName, value]];
    }
    free(ivar);
    
    [desc appendString:@"}"];
    return desc;
}

你可能感兴趣的:(【开发技巧】KVC的利用:1,实现归档中的 encodeWithCoder 和 initWithCoder方法)