首先我要说英语不是我的母语,可能我的语法不正确,但我会尽力解释我的restkit发生了什么。
我最近自己学习 restkit ,这是一个非常大的话题而且很难。
我正在使用此api。
我需要尝试在" current_condition'中获取 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 is Clear 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];
}
我觉得我的密钥路径错误,因为current_condition不包含天气,我也不知道如何修复它。
任何人都可以帮助我吗?
答案 0 :(得分:0)
你表现很好,你的英语非常好!
目前,您正在创建2个不同的对象管理器,可能会在此之间感到困惑。您没有显示所有代码,但您必须提出2个请求,我猜您在两种情况下都使用[[RKObjectManager sharedManager] getObjectsAtPath:...
。
请注意sharedManager
将始终返回您创建的第一个管理器实例。因此,您创建并配置第二个管理器,但您从不使用它。
现在,您不需要2位经理。它们都使用相同的基本URL,因此您应该只使用一个管理器并将所有映射添加到它。现在,您只需要发出1个请求,即可提取所需的所有数据。
您需要更改使用映射结果的方式。不使用array
,而是使用dictionary
。字典中的键是响应描述符的关键路径,因此您可以从字典中获得您想要的内容。