0

我正在使用 RestKit,但无法正确映射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;
}

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

其他一切正常,但图像无法正常工作,因为它们与所有其他数据略有不同。

真的无法弄清楚答案,非常感谢任何帮助

4

1 回答 1

2

更改imageson的属性Feed

property (nonatomic, strong) NSArray *images;

并使用hasMany关系

[mapping hasMany:@"images" withMapping:[Images mapping]];
于 2013-05-22T23:27:05.917 回答