Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将数组映射到RestKIt.20中的核心数据_Ios_Objective C_Restkit_Object Object Mapping - Fatal编程技术网

Ios 将数组映射到RestKIt.20中的核心数据

Ios 将数组映射到RestKIt.20中的核心数据,ios,objective-c,restkit,object-object-mapping,Ios,Objective C,Restkit,Object Object Mapping,Iam正在尝试将Json映射到名为Place的自定义对象 托管对象位置类别: @interface Place : NSManagedObject @property (nonatomic, retain) NSString * icon; @property (nonatomic, retain) NSString * place_id; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain)

Iam正在尝试将Json映射到名为Place的自定义对象

托管对象位置类别:

@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"
      },
当我尝试映射照片、类型时出现的问题

我有一门摄影课

@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]];
但有一个例外或价值是照片是零
如何映射照片和类型请提供任何帮助

您有两个不使用KVC键路径的对象数组,“html\u属性”和“类型”。有关这种情况下映射的更多信息,请参阅此RestKit文章:

但是——如果您可以控制JSON结构,我建议创建两个新的Objective C类,可能命名为
HtmlAttribute
Type
,每个类都有一个属性,为这些类创建映射并在RestKit中设置属性。如果您能够做到这一点并改变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"} ],