我是xcode的新手,请指导我更正错误,我的代码是,
//Set our mapView
[MapViewC setRegion:myRegion animated:NO];
CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
addressOutlet=[dictionary valueForKey:@"Street"];
City=[dictionary valueForKey:@"City"];
State=[dictionary valueForKey:@"State"];
if (addressOutlet!=NULL&&City!=NULL)
{
NSString *SubTitle=[NSString stringWithFormat:@"%@,%@,%@",addressOutlet,City,State];
cell.detailTextLabel.text=SubTitle;
}
else if (addressOutlet==NULL&&City!=NULL)
{
NSString *SubTitle=[NSString stringWithFormat:@"%@,%@,",City,State];
cell.detailTextLabel.text=SubTitle;
}
else if (addressOutlet!=NULL&&City==NULL)
{
NSString *SubTitle=[NSString stringWithFormat:@"%@,%@,",addressOutlet,State];
cell.detailTextLabel.text=SubTitle;
}
else if(addressOutlet==NULL&&City==NULL)
{
NSString *SubTitle=[NSString stringWithFormat:@"%@",State];
cell.detailTextLabel.text=SubTitle;
}
}];
提前致谢。
答案 0 :(得分:0)
确定数组可以为空,在这种情况下,以下语句将导致异常:
NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
// ^
所以要防范它,比如:
if ([placemarks count] > 0) {
NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
....
}
我还没有阅读该完成处理程序的API文档,但如果error
不是nil
,您可以可能测试并相应地采取行动(即, error != nil
响应错误,否则处理placemarks
数组。