如何用地幔在NSArray中指定子对象类型?

时间:2022-02-06 21:20:44

If I have a dictionary like

如果我有一本字典

{
  name: "Bob",
  cars: [
    { make: "ford", year: "1972" },
    { make: "mazda", year: "2000" }
  ],
}

and two models like:

和两个模型:

@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

How do I use Mantle so that my array of cars in my person model are CarModels?

我如何使用地幔让我的人模型中的汽车都是汽车模型?

5 个解决方案

#1


20  

Ah figured it out. I needed to add a private method:

呀算出来。我需要添加一个私有方法:

+ (NSValueTransformer *)carsTransformer
{
    return [NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:[CarModel class]];
}

and make sure I used:

确保我用过:

[PersonModel modelWithExternalRepresentation:dict];

#2


13  

+[NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:] is deprecated. The new API is +[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:].

+(NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:]是弃用。新的API是+[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:]。

After switching to the new API, the models can be initialized with the default initializers provided by, e.g., MTLJSONAdapter.

在切换到新的API之后,可以使用MTLJSONAdapter等提供的默认初始化器对模型进行初始化。

#3


12  

A note on:

报告:

[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CarModel.class];

This methods seems to now be deprecated. I'm using this new method in my code and it appears to be working just fine:

现在似乎不赞成使用这些方法。我在我的代码中使用了这个新方法,它似乎运行得很好:

[MTLJSONAdapter arrayTransformerWithModelClass:CarModel.class];

#4


4  

+ (NSValueTransformer *)carsJSONTransformer {
    return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CarModel.class];
}

Read more here

点击这里了解更多内容

#5


0  

+[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:]

This method is deprecated. New method is:

这个方法是不赞成的。新方法是:

  + (NSValueTransformer *)carsTransformer
    {
        return [MTLJSONAdapter arrayTransformerWithModelClass:[CarsModel class]];
    }

#1


20  

Ah figured it out. I needed to add a private method:

呀算出来。我需要添加一个私有方法:

+ (NSValueTransformer *)carsTransformer
{
    return [NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:[CarModel class]];
}

and make sure I used:

确保我用过:

[PersonModel modelWithExternalRepresentation:dict];

#2


13  

+[NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:] is deprecated. The new API is +[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:].

+(NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:]是弃用。新的API是+[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:]。

After switching to the new API, the models can be initialized with the default initializers provided by, e.g., MTLJSONAdapter.

在切换到新的API之后,可以使用MTLJSONAdapter等提供的默认初始化器对模型进行初始化。

#3


12  

A note on:

报告:

[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CarModel.class];

This methods seems to now be deprecated. I'm using this new method in my code and it appears to be working just fine:

现在似乎不赞成使用这些方法。我在我的代码中使用了这个新方法,它似乎运行得很好:

[MTLJSONAdapter arrayTransformerWithModelClass:CarModel.class];

#4


4  

+ (NSValueTransformer *)carsJSONTransformer {
    return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CarModel.class];
}

Read more here

点击这里了解更多内容

#5


0  

+[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:]

This method is deprecated. New method is:

这个方法是不赞成的。新方法是:

  + (NSValueTransformer *)carsTransformer
    {
        return [MTLJSONAdapter arrayTransformerWithModelClass:[CarsModel class]];
    }