关于模型成员变量是其他模型类型时KVC使用问题

//Amodel的h及m文件代码

@class Bmodel;

@interface Amodel :NSObject

@property (nonatomic, strong)Bmodel *model;

@end

@implementation Amodel

- initWithDic:(NSDictionary *)dic {

       if (self = [super init]) {

              [self setValuesForDictionary:dic];

       }

}

- setModel:(NSDictionary *)dic {

        Bmodel *model = [Bmodel modelWithDic:dic];

        self.model = model;

}

@end

//Bmodel的m文件部分代码

@implementation

- (Bmodel *)modelWithDic:(NSDictionary *)dic {

         return [Bmodel alloc] initWithDic:dic];

}

- initWithDic:(NSDictionary *)dic {

        if (self = [super init]) {

              [self setValuesForDictionary:dic];

        }

}

@end

//以上代码会崩溃  而且如果第一次找到崩溃位置 将崩溃代码注释掉 就会在其他(不做具体描述了)地方崩溃  原因是KVC不能识别自己建立的类型;  遇到这种情况 可以使用JsonModel三方库 或者故意将类型为model类型的名字写错  然后重写 - valueForUnfindKey :   -setValue:ForUnFindKey:方法  在第二个方法中重新赋值

(以上代码纯手写 可能会有很多单词字母错误的地方  我尽然懒到不想command + c/v ☀️)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容