2012-04-13 3 views
1

충돌 로그를 이해하려고합니다. CLLocationDistance 방법 distanceFromLocation을 수행하려고 할 때 발생합니다. 지오 코딩해야하는 두 위치를 가져 오려면 하나의 블록이 필요하므로이 두 인스턴스 변수를 가질 수 있습니다. 그래서 매개 변수 방법에 직접 : 그것은 distanceFromLocation 부분에 EXC_BAD_ACCESS (code =EXC_ARM_DA_ALIGN, address=.....etc....)을 제공합니다/충돌충돌 로그를 이해하지 못 했습니까?

- (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"]; 




    }]; 
} 

. 오류 로그의 스크린 샷을 첨부했습니다.

Crash Log

무엇이 발생할 수 있습니다이며 어떻게 해결합니까?

답변

2

나는 당신이 당신에게 라인 2와 3 체인에 의해 충돌을 피할 수

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

로 포장했기 때문에 LOCATION1 또는 LOCATION2가 설정되어 있지 않은 가정 :

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

을 어쩌면 당신은 필요 또한 변경 라인 :

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

난 당신이 적어도 하나의 NUL로 distanceFromLocation:를 실행 생각 L 매개 변수 또는 개체.

+0

예. location1 && location2가 0이 아닌 메소드를 기록한 경우 표시되지 않습니다 ... 오류가 발생하여 별도의 if (오류) 문에 오류가 발생했습니다. – MCKapur

+0

이제 충돌이 없습니다. btw ..... – MCKapur

+0

하지만 텍스트가 설정되어 있지 않습니다. 도 수정되었습니다. – MCKapur

관련 문제