美文网首页
如何利用 YYModel 来解析嵌套模型

如何利用 YYModel 来解析嵌套模型

作者: 请叫我大帅666 | 来源:发表于2017-04-23 11:56 被阅读0次

    项目开发中在所难免的要对获取到的数据进行模型嵌套分析,一层两层还好,但是多了,对于一些初学者,就会很头疼。

    今天我们说一下如何利用 YYModel 来解析嵌套模型,以省市区为例:

    1.先对模型嵌套分析:

    假设我们最初拿到的数据是一个装着省模型(provinceModel)的字典数组,里面有:省名字 NSString *province,

    children (城市模型 cityModel) 字典数组;  在城市模型中有:城市名字 NSString *city, children (区模型 districtModel) 的字典数组;区模型中装着区名字。

    2.利用 YYModel 对嵌套模型进行解析:

    1.最外层:provinceModel(省模型) : NSString *province ,  NSArray *children (里面装的是城市模型),也需要做字典装模型,所以要在 .m 中实现协议的这个方法:

    + (NSDictionary *)modelContainerPropertyGenericClass {

    return @{@“children”:[cityModel class]};

    }

    返回 Model 属性容器中需要存放的对象类型,YYModel 会自动进行处理

    2.第二层:cityModel (城市模型) : NSString *city ,  NSArray *children (里面装的是区模型),也需要做字典转模型,故也要在 .m 中实现协议的这个方法:

    + (NSDictionary *)modelContainerPropertyGenericClass {

    return @{@“children”:[districtModel class]};

    }

    返回 Model 属性容器中需要存放的对象类型,YYModel 会自动进行处理

    3.第三层:districtModel (区模型) : NSString *district.

    这样一层一层解析出来就会很清晰了,希望对大家有帮助

    相关文章

      网友评论

          本文标题:如何利用 YYModel 来解析嵌套模型

          本文链接:https://www.haomeiwen.com/subject/tozezttx.html