简体   繁体   English

如何使用Mantle将数组合并到模型对象中?

[英]How to Incorporate an Array Into Model Object With Mantle?

I have a JSON object like so: 我有一个像这样的JSON对象:

{
   "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 . 我可以验证MYModel对象的name设置正确,但是images设置为null How can I populate an array of strings with Mantle? 如何用Mantle填充字符串数组?

Update: Apparently mtl_externalRepresentationArrayTransformerWithModelClass: is deprecated. 更新:显然不建议使用mtl_externalRepresentationArrayTransformerWithModelClass: This might work: 这可能起作用:

[MTLJSONAdapter arrayTransformerWithModelClass:[NSString class]]; 

You need to specify value transformer for key images as Array value transformer. 您需要将关键images值转换器指定为数组值转换器。 You can do this with class method (on your MyModel class) with correct name. 您可以使用具有正确名称的类方法(在MyModel类上)执行此操作。 Something like this might work. 这样的事情可能会起作用。 I have not tested the code. 我没有测试代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM