【问题标题】:Restkit Mapping a NULL DataRestkit 映射 NULL 数据
【发布时间】:2014-07-16 06:09:35
【问题描述】:

首先我必须说英语不是我的母语,可能我的语法不正确,但我会尽力解释我的 restkit 发生了什么。

我最近在自学restkit,真的是一个很大的话题,很难。

我正在使用这个api

我需要尝试在 'current_condition' 和 'weather' 中获取 weatherDesc 的值

这是我映射 current_condition 的代码:

-(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);

}

-(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:[restkitCurrentCondition 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 weatherDesc
RKObjectMapping *weatherMapping = [RKObjectMapping mappingForClass:[restKitWeather class]];
[weatherMapping addAttributeMappingsFromDictionary:@{@"weatherDesc": @"myweatherDesc"}];
[currentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"weather" toKeyPath:@"weather" withMapping:weatherMapping]];
}

还不错,它说:

2014-07-15 12:14:26.702 myRestSample[10961:60b] /////////////////////湿度为:59 2014-07-15 12:14:26.702 myRestSample[10961:60b] ////////////////// 云层为:16 2014-07-15 12:14:26.703 myRestSample[10961:60b] //////////////weatherDesc 很清晰 2014-07-15 12:14:26.703 myRestSample[10961:60b] ///////// 天气中的 weatherDesc 为 (null) 2014-07-15 12:14:26.703 myRestSample[10961:60b] ///////// 天气中的 weatherDesc 为 (null)

为什么我得到 Null 数据?????????

我把这些方法放在viewdidload中

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  [self configureRestKit];
  [self loadCurrentCondition];

}

我感觉我的 keypath 是错误的,因为天气不包含在 current_condition 中,我不知道如何修复它。

谁能帮帮我?

【问题讨论】:

    标签: ios objective-c restkit


    【解决方案1】:

    你做得很好,你的英语也很好!

    目前您正在创建 2 个不同的对象管理器,并且可能会在这两者之间感到困惑。您没有显示所有代码,但您必须发出 2 个请求,我猜您在这两种情况下都使用[[RKObjectManager sharedManager] getObjectsAtPath:...

    请注意,sharedManager 将始终返回您创建的第一个管理器实例。因此,您创建并配置了第二个管理器,但您从不使用它。

    现在,您不需要 2 个经理。它们都使用相同的基本 URL,因此您应该只使用一个管理器并将所有映射添加到它。现在,您只需要发出 1 个请求,您想要的所有数据都会被提取出来。

    您需要更改使用映射结果的方式。不要使用array,而是使用dictionary。字典中的键是响应描述符的键路径,因此您可以从字典中准确获取所需的内容。

    【讨论】:

    • 我已经编辑了我的问题。你可以再检查一遍。我确实提出了 2 个请求。让我看看如何解决它
    • 试一试,如果您有更多问题,请更新问题,我们会做出一些更正。你学得很好,通过尝试会学得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2014-01-25
    相关资源
    最近更新 更多