- (NSString *) geocodeAddressFromCoordinate:(CLLocationCoordinate2D)coordinate
{
CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
__block NSMutableString * address = [NSMutableString string];
geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error) {
NSLog(@"%@", [error localizedDescription]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No results were found" message:@"Try another search" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
alert.show;
return;
}
if ([placemarks count]>0)
{
NSLog([placemarks description]);
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(placemark.locality);
//This line makes an error
[address initWithString:placemark.locality];**
}
}];
return address;
}
出现以下运行时错误:
*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* 初始化方法
-initWithCharactersNoCopy:length:freeWhenDone:无法发送到类__NSCFString的抽象对象:创建一个具体实例!
最佳答案
如果没有匹配的分配,则永远不要调用“initWithString:”。看起来更像你想要的[address setString:placemark.locality]
关于ios - 在 block 中分配NSString变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7742150/