ios - 在 block 中分配NSString变量时出错

标签 ios objective-c-blocks

    - (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/

相关文章:

ios - 通过短信和电子邮件发送 iPhone UUID

ios - 在同一线程上运行 block

objective-c - iOS - 单元测试异步代码

ios - 是否可以在返回 BOOL 的委托(delegate)方法中执行 API 调用?

objective-c - 声明对 block typedef 的前向引用?

iOS 主线程卡住几秒钟

ios - UITabBar 选择指示器 iPhone X 上的图像高度

ios - 批量 NSLayoutConstraint 激活/停用

ios - 这一行代码在 Swift 2+ 中的等价物是什么

ios - 包含对象 : returning NO when isEqual and hash match