【问题标题】:RestKit with XML - transitioning code to 0.20带有 XML 的 RestKit - 将代码转换为 0.20
【发布时间】:2014-03-27 17:41:25
【问题描述】:

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(城市为空)和genreArray(流派为空):

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?

【问题讨论】:

    标签: ios objective-c restkit restkit-0.20


    【解决方案1】:

    我不太会使用 XML,但可以尝试使用 nil 键路径映射:

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

    【讨论】:

    • 谢谢韦恩。这让我更进一步,但现在我看到一堆关于这些值的消息,如下所示: E restkit.object_mapping:RKMappingOperation.m:440 将 keyPath '(null)' 处的值转换为'NSString'类型的表示失败':错误域 = org.restkit.RKValueTransformers.ErrorDomain 代码 = 3002 “值 '{ text = Abilene; }' 到 NSString 的转换失败:所咨询的 2 个值转换器都没有成功。” UserInfo=0x15af2970 {detailedErrors=( "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"给定的值还不是...
    • 我想我需要将“.text”应用于 nil 键路径,有什么想法吗?
    • 喜欢这个? [genreMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"@text" toKeyPath:@"genre"]];当我改变它时它崩溃了。
    • 糟糕,它现在可以工作了:[genreMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"text" toKeyPath:@"genre"]];
    • 忘记感谢你了……所以谢谢你!关于最新的 RestKit 和解析 XML 的信息并不多。
    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多