0

无法从我的 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"
        }],

饲料.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;

饲料.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。

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

4

1 回答 1

1

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

@property (nonatomic, strong) NSArray *images;

您应该将映射设置为hasMany:.

于 2013-05-10T17:05:07.393 回答