我使用 wunderground 将天气api用于我当前的位置。当API中没有当前位置城市时,应用程序将崩溃。现在,当目前的位置城市不可用时,我想重定向附近的天气位置城市。
这是我的代码,
-(void) geoCodeUsingAddress:(NSString *)address
{
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString =[NSString stringWithFormat:@"http://api.wunderground.com/api/8e2edc55aaf7cfa7/geolookup/conditions/forecast/q/%@.json",esc_addr];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *alldicdata=[NSJSONSerialization JSONObjectWithData:response1 options:0 error:nil];
NSDictionary *forecast=[alldicdata objectForKey:@"forecast"];
NSDictionary *simpleforecast=[forecast objectForKey:@"simpleforecast"];
NSArray *arrofforecastday=[simpleforecast objectForKey:@"forecastday"];
NSMutableArray *dataStore1=[[NSMutableArray alloc]init];
for(NSDictionary *dict in arrofforecastday)
{
NSMutableDictionary *data=[[NSMutableDictionary alloc] init];
[data setObject:[dict objectForKey:@"conditions"] forKey:@"conditions"];
任何方式重定向或找到附近的城市。可以帮助我吗?
答案 0 :(得分:0)
查看错误回复:
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"geolookup": 1,
"conditions": 1,
"forecast": 1
},
"error": {
"type": "querynotfound",
"description": "No cities match your search query"
}
}
}
我实际上看不到您与我们分享的代码段会如何崩溃(因为如果出错,forecast
只会nil
,因此会simpleforecast
{{}} { {1}})。你真的应该测试其中任何一个是arrofforecastday
并优雅地处理这种情况。我怀疑你有一些后来的代码假设,例如,其中一个或多个不是nil
或你成功进入nil
循环。
更好(即除了以上几点),你应该测试服务器报告的错误,你应该说:
for