RestKit with XML - 将代码转换为0.20

时间:2014-03-27 17:41:26

标签: ios objective-c restkit restkit-0.20

RestKit 0.20的以下代码的等效语法是什么?

RKObjectMapping *atozMapping = [RKObjectMapping mappingForClass:[Atoz class]];
[atozMapping mapKeyPath:@"" toAttribute:@"city"];

RKObjectMapping *genreMapping = [RKObjectMapping mappingForClass:[Genres class]];
[genreMapping mapKeyPath:@"" toAttribute:@"genre"];

RKObjectMapping *stationSearchMapping = [RKObjectMapping mappingForClass:[StationSearchData class]];
[stationSearchMapping mapKeyPathsToAttributes:@"location", @"location", @"city", @"city", @"latitude", @"latitude", @"longitude", @"longitude", nil];
[stationSearchMapping mapKeyPath:@"browseatoz.atoz" toRelationship:@"browseAtozArray" withMapping:atozMapping];
[stationSearchMapping mapKeyPath:@"genres.genre" toRelationship:@"genreArray" withMapping:genreMapping];

[stationMapping mapRelationship:@"stationSearchData" withMapping:stationSearchMapping];
[stationManager.mappingProvider setMapping:stationSearchMapping forKeyPath:@"stationSearchData"];

这是我尝试过的,但它似乎没有正确加载browseAtozArray(city为null)和genreArray(genre为null):

RKObjectMapping *atozMapping = [RKObjectMapping mappingForClass:[Atoz class]];
[atozMapping addAttributeMappingsFromDictionary:@{
                                                      @"": @"city",
                                                      }];

RKObjectMapping *genreMapping = [RKObjectMapping mappingForClass:[Genres class]];
[genreMapping addAttributeMappingsFromDictionary:@{
                                                  @"": @"genre",
                                                  }];

RKObjectMapping *stationSearchMapping = [RKObjectMapping mappingForClass:[StationSearchData class]];
[stationSearchMapping addAttributeMappingsFromDictionary:@{
                                                  @"location.text": @"location",
                                                  @"city.text": @"city",
                                                  @"latitude.text": @"latitude",
                                                  @"longitude.text": @"longitude"
                                                  }];
RKRelationshipMapping* relationShipatozMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"browseatoz.atoz" toKeyPath:@"browseAtozArray" withMapping:atozMapping];
[stationSearchMapping addPropertyMapping:relationShipatozMapping];
RKRelationshipMapping* relationShipGenresMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"genres.genre" toKeyPath:@"genreArray" withMapping:genreMapping];
[stationSearchMapping addPropertyMapping:relationShipGenresMapping];

[stationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"stationSearchData" toKeyPath:@"stationSearchData" withMapping:stationSearchMapping]];

[stationManager addResponseDescriptor: [RKResponseDescriptor responseDescriptorWithMapping:stationSearchMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"stationSearchData" statusCodes:[NSIndexSet indexSetWithIndex:200]]];

这是相关的XML:

<stationSearchData>
   <location>
  <![CDATA[ 79605 ]]>
 </location>
 <city>
  <![CDATA[ Abilene ]]>
 </city>
 <latitude>
  <![CDATA[ 32.43892288 ]]>
 </latitude>
 <longitude>
  <![CDATA[ -99.77902222 ]]>
 </longitude>
 <browseatoz>
  <atoz>
   <![CDATA[ Abilene ]]>
  </atoz>
  <atoz>
   <![CDATA[ Texas ]]>
  </atoz>
 </browseatoz>
 <genres>
  <genre>
   <![CDATA[ Rock ]]>
  </genre>
 </genres>
 <personalities/>
</stationSearchData>

此外,这个问题似乎与我遇到的问题相同(除了它的解决方案是我用于旧版本的RestKit,并且不适用于0.20):How to map XML textContent with RestKit?

1 个答案:

答案 0 :(得分:1)

我不太习惯使用XML,但看看尝试使用nil密钥路径映射:

[genreMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"genre"]];