0

JSON応答から画像を正しくマッピングできず、その理由がわかりません。

ここにエラーがあります: "W restkit.object_mapping:RKObjectMappingOperation.m:244 キーパス '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;
}

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

画像以外はすべて正しくマッピングされています。それが配列であることと関係があるかどうかはわかりませんが、結果は1つしかないので、必ずしも配列であるとは限りませんか? そうでない場合、それをどのように書くか... マッピングしない理由が完全にはわかりません。

ご覧のとおり、両方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

imagesFeed.h で、プロパティを次のように変更する必要があります。

@property (nonatomic, strong) NSArray *images;

そして、マッピングを に設定する必要がありますhasMany:

于 2013-05-10T17:05:07.393 に答える