1

クラッシュログを理解しようとしています。CLLocationDistanceメソッドを実行しようとすると発生しますdistanceFromLocation。2 つの場所を取得するには、ジオコーディングする必要がありますが、これら 2 つのインスタンス変数を持つことができるように 1 つのブロックが必要です。したがって、パラメーターを使用してメソッドに指示します。

  - (void)geoCodeSetup:(NSArray *)placemarks :(NSError *)error andIdentifier:(NSString *)string {
CLLocation *location1;
CLLocation *location2;
if ([string isEqualToString:@"Origin"]) {
    if (!error) {
        CLPlacemark *place = [placemarks objectAtIndex:0];
        CLLocation *location = place.location;
        location1 = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];

    } else {
        NSString *string = [NSString stringWithFormat:@"%@ - also make sure you have inputted a valid city", [error localizedDescription]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

if ([string isEqualToString:@"Destination"]) {
    if (!error) {
        CLPlacemark *place = [placemarks objectAtIndex:0];
        CLLocation *location = place.location;
       location2 = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];


    } else {
        NSString *string = [NSString stringWithFormat:@"%@ - also make sure you have inputted a valid city", [error localizedDescription]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}


CLLocationDistance distanceM = [location1 distanceFromLocation:location2];    
[metreDistance setText:[NSString stringWithFormat:@"%f", distanceM]];

}

- (IBAction)calculateDistance {

[textFieldDestination resignFirstResponder];
NSString *origin = [textFieldOrigin text];
if (origin) {
    CLGeocoder *geoCode = [[CLGeocoder alloc] init];

    [geoCode geocodeAddressString:origin completionHandler: ^(NSArray *placemarks, NSError *error) {
        NSArray *p1 = placemarks;
        NSError *e1 = error;
        [p1 retain];
        [e1 retain];
        [self geoCodeSetup:p1 :e1 andIdentifier:@"Origin"];


    }]; 
}

NSString *destination = [textFieldDestination text];
if (destination) {

    CLGeocoder *geoCode2 = [[CLGeocoder alloc] init];

    [geoCode2 geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error) {
        NSArray *p2 = placemarks;
        NSError *e2 = error;
        [p2 retain];
        [e2 retain];
        [self geoCodeSetup:p2 :e2 andIdentifier:@"Destination"];




    }]; 
}

EXC_BAD_ACCESS (code =EXC_ARM_DA_ALIGN, address=.....etc....)distanceFromLocation 部分でクラッシュ/ギブします。クラッシュログのスクリーンショットを添付しました。

クラッシュログ

何が原因で、どうすれば解決できますか?

4

1 に答える 1

2

あなたがそれをラップしたので、location1またはlocation2が設定されていないと思います

if ([string isEqualToString:@"Origin"]) {
    if (!error) {

2 行目と 3 行目を次のようにチェーンすることで、クラッシュを回避できます。

CLLocation *location1 = nil;
CLLocation *location2 = nil;

また、次の行も変更する必要があるかもしれません。

CLLocationDistance distanceM = [location1 distanceFromLocation:location2]; 
// change to
CLLocationDistance distanceM;
if(location1 && location2) {
  distanceM = [location1 distanceFromLocation:location2];
}

distanceFromLocation:少なくとも 1 つの NULL パラメータまたはオブジェクトを使用して実行していると思います。

于 2012-04-13T10:06:14.780 に答える