在我的 iOS 应用程序中,我正在使用 worldweatheronline.com 的天气 API,但我得到一个随机的“EXC_BAD_ACCESS”错误哦,这一行(并非总是但有时):
temperature.text = [NSString stringWithFormat:@"%@ °C", tempC];
这是我的代码:
- (void)showWeatherFor:(CLLocation *)newLocation
{
NSString *myRequestString = [[NSString alloc] initWithFormat:@"http://api.worldweatheronline.com/free/v1/weather.ashx?q=%f,%f&format=json&num_of_days=5&key=ydvgep8jn5m846upd8kb2qp6", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myRequestString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *JsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *theDictionary = [NSDictionary dictionaryWithJSONString:JsonString error:&error];
NSDictionary *data = [theDictionary objectForKey:@"data"];
NSArray *currentDictionary = [data objectForKey:@"current_condition"];
NSDictionary *temp = [currentDictionary objectAtIndex:0];
if ([temp objectForKey:@"temp_C"] == nil || [temp objectForKey:@"temp_F"] == nil)
{
return;
termometro.alpha = 0;
}
else
{
if (temp != nil)
{
if ([mainDelegate.gradi isEqualToString: @"°C"])
{
NSNumber *tempC = [temp objectForKey:@"temp_C"];
temperature.text = [NSString stringWithFormat:@"%@ °C", tempC];
mainDelegate.temperature = [[temp objectForKey:@"temp_C"]intValue];
}
else
{
NSNumber *tempF = [temp objectForKey:@"temp_F"];
temperature.text = [NSString stringWithFormat:@"%@ °F", tempF];
mainDelegate.temperature = [[temp objectForKey:@"temp_F"]intValue];
}
termometro.alpha = 1;
}
}
}