使用restkit进行映射“如何使用多个映射和描述符

时间:2014-07-16 17:38:27

标签: ios objective-c mapping restkit

stackoverflow中的朋友们:

我在使用restkit从api映射数据时遇到问题。我真的想知道我的错误在哪里。

Json格式:

{
data: {
   -current_condition: [1]
       0:  {
           cloudcover: "16"
           humidity: "59"
          - weatherDesc: [1]
       0:  {
            value: "Clear"
          }

- weather: [5]
0:  {
    tempMinC: "10"
    tempMinF: "50"
    weatherCode: "119"
  -  weatherDesc: [1]
0:  {
     value: "Cloudy"
     }
    .......
}

这是我的代码如何进行映射(我尝试映射' cloudcover,湿度'以及当前条件和天气中的' weatherDesc'

-(void)configureRestKit{

NSURL *baseURL = [NSURL URLWithString:@"http://www.raywenderlich.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

RKObjectMapping *currentMapping = [RKObjectMapping mappingForClass:[CurrentCondition class]];
[currentMapping addAttributeMappingsFromArray:@[@"cloudcover",@"humidity",@"weatherDesc"]];
[currentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"current_condition" toKeyPath:@"current_condition" withMapping:currentMapping]];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:currentMapping method:RKRequestMethodGET pathPattern:@"/demos/weather_sample/weather.php" keyPath:@"data.current_condition" statusCodes:[NSIndexSet indexSetWithIndex:200]];

[objectManager addResponseDescriptor:responseDescriptor];



//weahter Desc
RKObjectMapping *weatherMapping = [RKObjectMapping mappingForClass:[Weather class]];
[weatherMapping addAttributeMappingsFromDictionary:@{@"weatherDesc": @"myweatherDesc"}];
[weatherMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"weather" toKeyPath:@"weather" withMapping:weatherMapping]];

RKResponseDescriptor *weatherresponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:weatherMapping method:RKRequestMethodGET pathPattern:@"/demos/weather_sample/weather.php" keyPath:@"data.weather" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:weatherresponseDescriptor];

}


-(void)loadCurrentCondition{

  NSDictionary *queryParams = @{@"format": @"json"};
  [[RKObjectManager sharedManager] getObjectsAtPath:@"/demos/weather_sample/weather.php"  parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
 _myArr = mappingResult.array;
 [self Humidity];
 } failure:^(RKObjectRequestOperation *operation, NSError *error) {
   NSLog(@"The error is :%@",error);
 }];

}

-(void)Humidity{
  restkitCurrentCondition *rkCC = [_myArr objectAtIndex:0];
  NSLog(@"///////////////////////the humidity is: %ld",rkCC.humidity.longValue);
  NSLog(@"//////////////////// the cloudcover is: %ld",rkCC.cloudcover.longValue);
  NSLog(@"/////////////// the weatherDesc is %@",rkCC.weatherDesc[0][@"value"]);
  NSLog(@"///////// the weatherDesc in weather is %@",rkCC.restkitweather.myweatherDesc[0][@"value"]);
  NSLog(@"///////// the weatherDesc in weather is %@",rkCC.restkitweather.myweatherDesc);
}

这是我得到的:

2014-07-16 14:21:36.076 myRestSample[3783:60b] I restkit:RKLog.m:33 RestKit logging initialized...
2014-07-16 14:21:36.154 myRestSample[3783:60b] I 
restkit.network:RKObjectRequestOperation.m:150 GET  
'http://www.raywenderlich.com/demos/weather_sample/weather.php?format=json'
2014-07-16 14:21:36.289 myRestSample[3783:3a0f] I 
restkit.network:RKObjectRequestOperation.m:220 GET  
'http://www.raywenderlich.com/demos/weather_sample/weather.php?format=json' (200 OK / 6
 objects) [request=0.1323s mapping=0.0024s total=0.1525s]
 2014-07-16 14:21:36.289 myRestSample[3783:60b] -[Weather humidity]: unrecognized 
 selector sent to instance 0x8f8eb30

我尝试将descritor的密钥路径改为' nil'看起来像这样

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:currentMapping 
method:RKRequestMethodGET 
pathPattern:@"/demos/weather_sample/weather.php" keyPath:nil 
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

Weather和CurrentCondition是NSObject,在CurrentCondition中有四个属性(cloudcover,湿度,NSArray * weaterDesc,Weather * restkitweather).Weven.h中只有两个属性(NSArray * myweatherDesc,* weatherDesc)

似乎我已经有6件物品,但为什么我得到了[天气湿度]未经验证的'。 任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

  

'[天气湿度]未经评估'

这意味着您拥有Weather实例,但您将其视为当前条件实例。

根据我对您的其他问题的回答,您不应该使用_myArr = mappingResult.array;,因为您无法确定数组包含的内容。相反,您应该使用dictionary并根据关键路径根据需要提取正确类型的对象。