1

我有一种感觉,我的问题实际上与阻塞有关,但也许这也是其他问题。我正在尝试转发地址地理编码并将坐标放入数组中以供以后使用。

当我尝试调用我尝试添加到块中数组的对象之一时,底部会引发异常。在任何 NSLog 在块文本中打印出来之前,也会引发异常。

处理这个问题的正确方法是什么?谢谢。

 - (NSMutableArray *)convertAddressToGeocode:(NSString *)addressString
{
    //return array with lat/lng
    __block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:addressString
             completionHandler:^ (NSArray* placemarks, NSError* error) {
                 for (CLPlacemark* aPlacemark in placemarks)
                 {
                     // Process the placemark.
                     if (error){
                         NSLog(@"Geocode failed with error: %@", error);
                         [self displayError:error];
                         return;
                     }
                     else {

                         NSArray const *keys = @[@"coordinate.latitude",
                                                 @"coordinate.longitude",
                                                 @"altitude",
                                                 @"horizontalAccuracy",
                                                 @"verticalAccuracy",
                                                 @"course",
                                                 @"speed",
                                                 @"timestamp"];

                         NSString *keylat = keys[0];
                         NSString *keylng = keys[1];

                         if (aPlacemark.location == nil)
                         {
                             NSLog(@"location is nil.");

                         }
                         else if ([keylat isEqualToString:@"coordinate.latitude"] && [keylng isEqualToString:@"coordinate.longitude"])
                         {
                             NSString *lat = @"";
                             NSString *lng = @"";
                             lat = [self displayStringForDouble: [aPlacemark.location coordinate].latitude];
                             lng = [self displayStringForDouble: [aPlacemark.location coordinate].longitude];

                             NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED
                             [coordinates addObject:[NSString stringWithFormat:@"%@",lat]];
                             [coordinates addObject:[NSString stringWithFormat:@"%@",lng]];
                         }}}}];
NSLog(@"Array: %@", coordinates[0]);  //EXCEPTION RAISED HERE, Nothing ever gets added
return coordinates;
}

这是应该插入此方法的代码,但我没有从 convertAddresstoGeocode 中获取坐标以传递给 convertCoordinatestoRepModel:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSString *addressToSearch = self.addressSearchField.text;
    NSMutableArray *coordinateArray = [self convertAddressToGeocode:addressToSearch];
    NSMutableArray *repModelArray = [self convertCoordinatesToRepModel:coordinateArray];
...
}
4

2 回答 2

1

如果geocodeAddressString是异步操作,那么您的块将在之后执行

NSLog(@"Array: %@", coordinates[0]);

此外,在您的方法调用结束后(当事件已经处理时)释放坐标数组(这是因为 __block 修饰符 - 块不保留具有 __block 修饰符的对象),并且在您的块中您尝试使用释放的coordinates数组。

再次:

您的块将在之后被调用NSLog(@"Array: %@", coordinates[0]);

费:

  1. 删除NSLog(@"Array: %@", coordinates[0]);- 在那一刻数组为空是正常的。
  2. 将您的coordinates数组存储在 some 中@property,您可以在使用 in 块后释放它

更新:

在 .h 文件中

typedef void (^ConverteArrayCallback)(NSArray *coordinates); 

在下面@intrerface

- (void)convertAddressToGeocode:(NSString *)addressString callback:(ConverteArrayCallback) callback;

在 .m 文件中

- (void)convertAddressToGeocode:(NSString *)addressString callback:(ConverteArrayCallback) callback { 
    NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:addressString
             completionHandler:^ (NSArray* placemarks, NSError* error) {
                 for (CLPlacemark* aPlacemark in placemarks)
                 {
                     // Process the placemark.
                     if (error){
                         NSLog(@"Geocode failed with error: %@", error);
                         [self displayError:error];
                         return;
                     }
                     else {

                         NSArray const *keys = @[@"coordinate.latitude",
                                                 @"coordinate.longitude",
                                                 @"altitude",
                                                 @"horizontalAccuracy",
                                                 @"verticalAccuracy",
                                                 @"course",
                                                 @"speed",
                                                 @"timestamp"];

                         NSString *keylat = keys[0];
                         NSString *keylng = keys[1];

                         if (aPlacemark.location == nil)
                         {
                             NSLog(@"location is nil.");

                         }
                         else if ([keylat isEqualToString:@"coordinate.latitude"] && [keylng isEqualToString:@"coordinate.longitude"])
                         {
                             NSString *lat = @"";
                             NSString *lng = @"";
                             lat = [self displayStringForDouble: [aPlacemark.location coordinate].latitude];
                             lng = [self displayStringForDouble: [aPlacemark.location coordinate].longitude];

                                 NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED
                                 [coordinates addObject:[NSString stringWithFormat:@"%@",lat]];
                                 [coordinates addObject:[NSString s

tringWithFormat:@"%@",lng]];
                                 }}}

                           if (callback != NULL) {
                                 callback(coordinates);
                           }

      }];
}

那应该行得通!

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSString *addressToSearch = self.addressSearchField.text;
    [self convertAddressToGeocode:addressToSearch callback:^(NSArray *coordinates)
      {  
       self.textView.text = [coordinates objectAtIndex:0];
      }];
}
于 2013-10-10T05:44:04.750 回答
-1
__block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0];

问题就在这里;只需将上面的代码替换为:

NSMutableArray *coordinates = [[NSMutableArray alloc] init];
于 2013-10-10T05:34:06.833 回答