1

これが私のコードと、reverseGeocodeLocation を呼び出そうとする完全なコンテキストです。

CLLocationCoordinate2D touchMapCoordinate = 
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

CLLocation *destination = [[CLLocation alloc] initWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];

__block CLGeocoder *destinationPin = [[CLGeocoder alloc] init];
__block NSString *destinationPinName;

[destinationPin reverseGeocodeLocation:destination completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error){
        NSLog(@"Geocode failed with error: %@", error);
        return;
    }
    if(placemarks && placemarks.count > 0)
    {
        CLPlacemark *topResult = [placemarks objectAtIndex:0];

        NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@",
                                [topResult subThoroughfare],[topResult thoroughfare],
                                [topResult locality], [topResult administrativeArea]];

        destinationPinName = [[NSString alloc] initWithString:addressTxt];
    }
}];

annotation = [[MQAnnotation alloc] initWithTitle:destinationPinName andCoordinate:touchMapCoordinate];

関数のエントリ ポイント、関数内の両方の if ブロック内、および注釈を割り当てる関数の後にブレークポイントを設定しました。reverseGeocodeLocation ブロックに到達すると、destinationPin には有効な値がありますが、プログラムは関数呼び出しの後に設定されたブレーク ポイントに直接ジャンプし続けます。何が起こっているのでしょうか??

4

1 に答える 1

1

の実行reverseGeocodeLocationは非同期で行われます。そのバックグラウンドタスクが完了すると、完了ブロックが呼び出されます。つまり、あなたが見ているのは正しい振る舞いです。の呼び出しreverseGeocodeLocationは、完了するずっと前、および完了ハンドラーが呼び出されるずっと前に、すぐに戻ります。アノテーションが完了ハンドラーで作成されるように、コードを更新する必要があります。

于 2012-10-22T14:32:41.877 に答える