【发布时间】: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