如果我有这样的字典
{
name: "Bob",
cars: [
{ make: "ford", year: "1972" },
{ make: "mazda", year: "2000" }
],
}
还有两个模型:
@interface CarModel : MTLModel
@property (nonatomic, strong) NSString *make;
@property (nonatomic, strong) NSString *year;
@end
@interface PersonModel : MTLModel
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *cars;
@end
如何使用 Mantle,以便我的个人模型中的汽车数组成为 CarModels?
How do I use Mantle so that my array of cars in my person model are CarModels?
啊,想通了.我需要添加一个私有方法:
Ah figured it out. I needed to add a private method:
+ (NSValueTransformer *)carsTransformer
{
return [NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:[CarModel class]];
}
并确保我使用过:
[PersonModel modelWithExternalRepresentation:dict];
这篇关于如何使用 Mantle 在 NSArray 中指定子对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!