2013-07-29 82 views
0

蔭試圖映射JSON來調用地點映射數組到核心數據在RestKIt 0.20

管理對象類的地方自定義對象:在這種甲酸

@interface Place : NSManagedObject 

@property (nonatomic, retain) NSString * icon; 
@property (nonatomic, retain) NSString * place_id; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSNumber * price_level; 
@property (nonatomic, retain) NSNumber * rating; 
@property (nonatomic, retain) NSString * vicinity; 
@property (nonatomic, retain) NSString * reference; 
@property (nonatomic, retain) Geometry *geometry; 
@property (nonatomic, retain) Json *json; 
@property (nonatomic, retain) NSSet *opening_hours; 
@property (nonatomic, retain) NSSet *photos; 
@property (nonatomic, retain) NSSet *types; 
@end 

和JSON:

"results" : [ 
     { 
     "geometry" : { 
      "location" : { 
       "lat" : -33.870540, 
       "lng" : 151.1988150 
      } 
     }, 
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png", 
     "id" : "c71365287e7606bd21a3311d21fda087830b7813", 
     "name" : "Pancakes on the Rocks", 
     "opening_hours" : { 
      "open_now" : true 
     }, 
     "photos" : [ 
      { 
       "height" : 1224, 
       "html_attributions" : [ 
        "\u003ca href=\"https://plus.google.com/105663944571530352563\"\u003eJoshua Gilmore\u003c/a\u003e" 
       ], 
       "photo_reference" : "CnRoAAAAtHLK7ii6I9RN0soDFHF9Zqu1ppyHouUPu-E_BlAP2xlJJLMJrlsBBr3ALzYZ_ysFdgDzJOc-L3NkqQ2FLk5nOAW7LNldTthkSslmbXkXqGbScwCzAwgIj_EyUQlGQjS6d7Ng36nKy_SItlejfeR8zRIQYtT--IxV_d-GfdTcebDjfhoUU7gE_ufZOBxH35EPUtUXbCJk9Gs", 
       "width" : 1632 
      } 
     ], 
     "price_level" : 2, 
     "rating" : 3.80, 
     "reference" : "CoQBcgAAAI_28BshREfmXJ9UKzOLLalhpRaqcW-Wupk1-D2sgkU6ZNe1nsR0zXopB5E-_BGXO1gHxJ1IAe0l-GXzrXj9Dz31crwQ-iwNFLcBRKSGnLmEu_AgKCkfDVZIsgeGrXLNxWLOZ_U8WAZzJu5Uc9tHa9LUF2Rj1MPk9vroGcFjLGWMEhCynlHHTt_P0EZ4wSwwfsumGhQAkqN53-D4ZNjBPEr7qJXZAZMdDg", 
     "types" : [ "cafe", "restaurant", "food", "establishment" ], 
     "vicinity" : "Harbourside Shopping Centre,Darling Harbour/227 & 229-230 Darling Drive, Sydney" 
     }, 

當我嘗試地圖照片的問題,類型

我有NSManagedObject類照片

@interface Photo : NSManagedObject 

@property (nonatomic, retain) NSNumber * height; 
@property (nonatomic, retain) NSNumber * width; 
@property (nonatomic, retain) NSString * photo_reference; 
@property (nonatomic, retain) NSSet *html_attributions; 
@property (nonatomic, retain) Place *place; 
@end 

我嘗試映射此:

RKEntityMapping* photosMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Photo class]) inManagedObjectStore:objectManager.managedObjectStore]; 

    [photosMapping addAttributeMappingsFromArray:@[@"height",@"photo_reference",@"html_attributions",@"width"]]; 

    //Add relationship between place and photos 
    [placeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"photos" 
                       toKeyPath:@"photos" 
                       withMapping:photosMapping]]; 

但有異常或價值的照片是零 請

回答

2

你有兩個數組我能如何映射照片和類型的任何幫助未使用KVC關鍵路徑的對象,「html_attributions」和「類型」。請參閱有關映射的更多信息這RestKit文章中這種類型的情況:Mapping values without key paths

但是 - 如果你有過的JSON結構控制,我建議創建兩個新的目標C類,或許命名爲HtmlAttributionType,每一個屬性,爲這些類創建映射,並在RestKit中設置relationships。如果你可以做到這一點,並改變你的JSON如何通過,它會爲你大大簡化事情。

可能的新JSON結構這些類(片段):

"html_attributions" : [ {"attribution_data":"\u003ca 
      href=\"https://plus.google.com/105663944571\u003c/a\u003e"} ], 

"types" : [ {"type_name":"cafe"}, {"type_name":"restaurant"}, 
      {"type_name":"food"}, {"type_name":"establishment"} ],