How to Incorporate an Array Into Model Object With Mantle?

536 Views Asked by At

I have a JSON object like so:

{
   "name": "Brendan",
   "images": ["some.url.to.image1",
             "some.url.to.image2",
             "some.url.to.image3"]
}

My class is as follows:

@interface MyModel : MTLModel <MTLJSONSerializing>

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSArray *images;

@end

@implementation MYModel

+ (NSDictionary*)JSONKeyPathsByPropertyKey {
    return @{
             @"name" : @"name",
             @"images" : @"images"
             };
}

@end

I can verify that MYModel object has name properly set, but images is set to null. How can I populate an array of strings with Mantle?

1

There are 1 best solutions below

1
Montas On

Update: Apparently mtl_externalRepresentationArrayTransformerWithModelClass: is deprecated. This might work:

[MTLJSONAdapter arrayTransformerWithModelClass:[NSString class]]; 

You need to specify value transformer for key images as Array value transformer. You can do this with class method (on your MyModel class) with correct name. Something like this might work. I have not tested the code.

+ (NSValueTransformer *)imagesTransformer
{
    return [NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:[NSString class]];
}