2013-05-10 45 views
0

我是無法正確映射來自我的JSON響應的圖像,我找不到原因。RestKit沒有映射來自JSON的圖片

這裏是錯誤:「W restkit.object_mapping:RKObjectMappingOperation.m:在的keyPath‘圖像’沒有戰略從‘__NSArrayM’轉變爲‘形象’的價值244轉型失敗」

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

其他的一切是正確的映射圖像除外。我不知道是否它是一個數組,但它只有一個結果,所以它不一定是一個數組?如果沒有,那麼我會怎麼寫...我只是不完全清楚爲什麼它不映射。

正如你所看到的,我嘗試了兩個hasMany:hasOne:映射,都沒有工作,我得到了同樣的錯誤。我試過使用NSLog(@"url image: %@", feedLocal.images.url);' but get(null). Even though I thought it would work because NSLog(@「href web:%@」,feedLocal.links.web.href);`完全適用於我的鏈接href。

任何幫助將不勝感激,非常感謝!

+0

Restkit不理解數組 - 使用可變形! – 2013-05-10 22:23:01

+0

@BharatGulati感謝您的迴應!我不確定這意味着什麼,你能給我一小段代碼嗎? – Realinstomp 2013-05-11 15:19:30

回答

1

在Feed.h你需要改變你的images屬性爲:

@property (nonatomic, strong) NSArray *images; 

你應該映射設置爲hasMany:

+0

感謝您的迴應!當我根據你的建議更改代碼時,我得到一個錯誤,說'在NSArray類型的對象上找不到屬性url',我應該做些什麼調整? – Realinstomp 2013-05-10 17:32:02

+0

'[mapping hasMany:@「images」withMapping:[Images mapping]];'是我改變了映射到 – Realinstomp 2013-05-10 17:37:18

+0

奇怪...嘗試跟蹤記錄RKLogConfigureByName(「RestKit/ObjectMapping」,RKLogLevelTrace); – Wain 2013-05-10 18:17:56