27

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?

0

5 Answers 5

21

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];
4
  • 6
    This method is deprecated, the new one is: [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:[CarModel class]];
    – KrauseFx
    Aug 23, 2013 at 19:14
  • It didn't seem to drop into the property transformer method until I parsed the JSON like this: MyModelClass * myModel = [MTLJSONAdapter modelOfClass:[MyModelClass class] fromJSONDictionary:jsonDictionary error:&error];
    – slim
    Aug 23, 2013 at 22:27
  • Don't forget #import <Mantle/NSValueTransformer+MTLPredefinedTransformerAdditions.h>
    – Kyle Clegg
    Jun 3, 2014 at 18:20
  • 3
    now you have to use [MTLJSONAdapter arrayTransformerWithModelClass:[CarModel class]];
    – gaRik
    Oct 14, 2015 at 16:59
13

+[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.

1
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
+ (NSValueTransformer *)carsJSONTransformer {
    return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:CarModel.class];
}

Read more here

2
  • This one works fine for me, and is simple and clear. Feb 2, 2015 at 22:36
  • @GoldThumb actually, Mantle grows over time, some of its API change, so we need to track that change if we plan to update Mantle
    – onmyway133
    Feb 3, 2015 at 2:54
0
+[NSValueTransformer mtl_JSONArrayTransformerWithModelClass:]

This method is deprecated. New method is:

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.