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"
        }],

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;
}

Images.h

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

+ (RKObjectMapping *) mapping;

Images.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 に答える