【问题标题】:RestKit not mapping Images from JSONRestKit 没有从 JSON 映射图像
【发布时间】:2013-05-10 16:59:39
【问题描述】:

无法正确映射 JSON 响应中的图像,我不知道为什么。

这是错误:“W restkit.object_mapping:RKObjectMappingOperation.m:244 keyPath 'images' 处的值转换失败。没有从 '__NSArrayM' 转换为 'Images' 的策略”

这里是 JSON:

{
    "timestamp": "2013-05-10T03:09:39Z",
    "feed": [{
        "headline": "Head text",
        "links": {
            "api": {
                "news": {
                    "href": "http://api.website.com/v1/91"
                },
                "self": {
                    "href": "http://api.website.com/v1/91"
                }
            },
            "web": {
                "href": "http://website.com/the/story"
            },
            "mobile": {
                "href": "http://m.website.com/wireless/story?storyId=9254113"
            }
        },
        "source": "Associated Press",
        "description": "Description text.",
        "images": [{
            "height": 324,
            "alt": "",
            "width": 576,
            "name": "Name text",
            "caption": "Caption text.",
            "url": "http://a.website.com/media/2013/0508.jpg"
        }],

Feed.h

@property (nonatomic, strong) Links *links;
@property (nonatomic, strong) Images *images;
@property (nonatomic, strong) Video *video;
@property (nonatomic, strong) NSString *headline;
@property (nonatomic, strong) NSString *source;
@property (nonatomic, strong) NSDate *published;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *premium;

+ (RKObjectMapping *) mapping;

Feed.m

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"headline", @"headline",
         @"source", @"source",
         @"published", @"published",
         @"description", @"description",
         @"premium", @"premium",
         nil];
        [mapping hasOne:@"links" withMapping:[Links mapping]];
        [mapping hasOne:@"images" withMapping:[Images mapping]];
        //[mapping hasMany:@"images" withMapping:[Images mapping]];
        [mapping hasOne:@"video" withMapping:[Video mapping]];
    }];

    return objectMapping;
}

图片.h

@property (nonatomic, strong) NSNumber *height;
@property (nonatomic, strong) NSNumber *width;
@property (nonatomic, strong) NSString *caption;
@property (nonatomic, strong) NSURL *url;

+ (RKObjectMapping *) mapping;

图片.m

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"height", @"height",
         @"width", @"width",
         @"caption", @"caption",
         @"url", @"url",
         nil];
    }];

    return objectMapping;
}

除图像外,其他一切都正确映射。我不知道它是否与它是一个数组有关,但它只有一个结果,所以它不一定是一个数组?如果不是,那我该怎么写……我只是不完全清楚为什么它不映射。

如您所见,我尝试了 hasMany:hasOne: 映射,但都没有成功,我得到了同样的错误。

我尝试使用 NSLog(@"url image: %@", feedLocal.images.url);' but get(null). Even though I thought it would work becauseNSLog(@"href web: %@", feedLocal.links.web.href);` 非常适合我的链接 href。

任何帮助将不胜感激,非常感谢!

【问题讨论】:

  • Restkit 无法理解数组 - 使用可转换的!
  • @BharatGulati 感谢您的回复!我不确定这到底是什么意思,你能给我一个sn-p代码吗?

标签: ios json restkit


【解决方案1】:

在 Feed.h 中,您需要将 images 属性更改为:

@property (nonatomic, strong) NSArray *images;

您应该将映射设置为hasMany:

【讨论】:

  • 感谢您的回复!当我根据您的建议更改代码时,我收到一条错误消息,提示 property url not found on object of type NSArray,我应该进行哪些其他调整?
  • [mapping hasMany:@"images" withMapping:[Images mapping]]; 是我将映射更改为
  • 奇怪...尝试跟踪日志记录 RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
  • 这是一个巧妙的提示。不过,我应该在这里寻找什么?
  • 如果我知道它会说什么,我不会对上述更改为什么不起作用感到困惑 :-) 与映射“图像”或“图像”有关。只是有关它在记录错误之前所做的事情的更多信息。
猜你喜欢
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
相关资源
最近更新 更多