How can we use YYModel to parse nested models?
During project development, it is inevitable to perform nested model analysis on the obtained data. It is better to have two layers at a time. However, it is a headache for beginners.
Today, let's talk about how to use YYModel to parse nested models. Take the province, city, and district as an example:
1. nested model analysis:
Assume that the data we first obtained is a dictionary array containing the province model (provinceModel), which contains the province name NSString * province,
The children (cityModel) dictionary array contains the NSString * city and children (districtModel) dictionary arrays. The district model contains the district name.
2. Use YYModel to parse the nested model:
1. outermost layer: provinceModel (province model): NSString * province, NSArray <cityModel *> * children (the city model is installed in it). You also need to build a dictionary model. m to implement the Protocol:
+ (NSDictionary *) modelContainerPropertyGenericClass {
Return @ {@ "children": [cityModel class]};
}
Returns the type of the object to be stored in the Model Property container. YYModel automatically processes the object.
2. layer 2: cityModel (city model): NSString * city, NSArray <districtModel *> * children, therefore. m to implement the Protocol:
+ (NSDictionary *) modelContainerPropertyGenericClass {
Return @ {@ "children": [districtModel class]};
}
Returns the type of the object to be stored in the Model Property container. YYModel automatically processes the object.
3. Layer 3: districtModel (Zone Model): NSString * district.
This layer-by-layer resolution will be very clear and I hope it will help you.