0

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?

Stunner
  • 12,025
  • 12
  • 86
  • 145
  • This ends up working as is. I ended up making the mistake of using the incorrect case of the JSONKey for `images` in my version of the above code. – Stunner Jun 11 '16 at 20:55

1 Answers1

0

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]];
}
Montas
  • 674
  • 6
  • 13
  • The code you provided doesn't compile due to `mtl_externalRepresentationArrayTransformerWithModelClass` not being found. I tried this `return [MTLJSONAdapter arrayTransformerWithModelClass:[NSString class]];` and the app merely crashes on that line. The console spits out: `*** Assertion failure in +[MTLJSONAdapter dictionaryTransformerWithModelClass:], /Users/dcaunt/Projects/Mantle/Mantle/MTLJSONAdapter.m:476`. – Stunner Jun 11 '16 at 11:50