2014-07-16 34 views
0

朋友你好在計算器:測繪與restkit「如何使用多個映射和描述符

我使用restkit.I真的想知道我的錯誤是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,溼度」,並在current_condition和天氣都「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的的keyPath更改爲「零」,看起來像這樣

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

天氣和CurrentCondition是NSObject的和有四個屬性在CurrentCondition中(雲覆蓋,溼度,NSArray * weaterDesc,Weather * restkitweather)。Weather.h中只有兩個屬性(NSArray * myweatherDesc,* weatherDesc)

它似乎我已經有6個物體,但爲什麼我得到了'[天氣溼度]無法估計'。 誰能幫助?請..

+0

你的類「restkitCurrentCondition」的定義是什麼(注意類名應該以大寫字母開頭) – Wain

+0

嘿,朋友!很高興再次見到你。它是一個NSObject,它有4個屬性(NSNumber * NSNumber *溼度,NSArray * weatherDesc,restKitWeather * restkiitweather) – TianMing

+0

@Wain我已經搜索了有關restkit的溢出問題。您是唯一答案的人............你能幫忙嗎我更多?我已經更新了我的問題。 – TianMing

回答

0

「[天氣溼度] Unreconized」

這意味着你有一個Weather實例,但你把它當作當前條件實例。

根據我對你的其他問題的回答,你不應該使用_myArr = mappingResult.array;,因爲你不能確定數組包含什麼。相反,您應該使用dictionary並根據關鍵路徑爲您的需求提取正確類型的對象。

+0

你真棒。你的答案是正確的。問題來自_myArr,我已經修復它。 – TianMing