How to specify child objects type in an NSArray with Mantle

后端 未结 5 1590
無奈伤痛
無奈伤痛 2021-01-31 17:30

If I have a dictionary like

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

相关标签:
5条回答
  • 2021-01-31 18:05

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

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

    0 讨论(0)
  • 2021-01-31 18:09
    + (NSValueTransformer *)carsJSONTransformer {
        return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CarModel.class];
    }
    

    Read more here

    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2021-01-31 18:16

    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];
    
    0 讨论(0)
  • 2021-01-31 18:26
    +[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:]
    

    This method is deprecated. New method is:

      + (NSValueTransformer *)carsTransformer
        {
            return [MTLJSONAdapter arrayTransformerWithModelClass:[CarsModel class]];
        }
    
    0 讨论(0)
提交回复
热议问题