2011-10-12 34 views
1
- (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; 
    } 

了以下运行时错误:没有匹配的页头:错误当一个块分配的NSString变量

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* initialization method -initWithCharactersNoCopy:length:freeWhenDone: cannot be sent to an abstract object of class __NSCFString: Create a concrete instance!'

回答

3

你永远不应该叫“initWithString”。看起来更像是你想要的是[address setString:placemark.locality]

+0

它停止崩溃,但地址为零:( – Shmidt

+0

我发现块完成后功能,这就是为什么NSString是空的。你可以请任何建议吗? – Shmidt

3

您已经使用这一行[NSMutableString string];所以你要[address initWithString:placemark.locality];呼叫尝试初始化已经初始化的对象初始化address

更改此:

[address initWithString:placemark.locality]; 

要:

[address setString:placemark.locality]; 

NSString Class Reference
NSMutableString Class Reference

1

在这一点上,你的字符串已经被初始化,[的NSMutableString字符串]是一个便利的方法ial返回[[[[NSMutableString alloc] init] autorelease]。您正在尝试重新初始化一个已经被inited的对象,这是不好的。

将该行更改为[address appendString:placemark.locality];

2
[address initWithString:placemark.locality]; 

应该是更象:

address = placemark.locality; 

[address appendString:placemark.locality]; 

取决于什么玉试图完成。

+0

使用块地址实例后返回零 – Shmidt