2012-04-13 159 views
1

我想了解崩潰日誌。當我嘗試執行CLLocationDistance方法distanceFromLocation時,會發生這種情況。爲了獲得兩個位置,我需要進行地理編碼,但我需要一個塊,以便可以擁有這兩個實例變量。所以我把它帶參數直接的方法:不明白崩潰日誌?

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




    }]; 
} 

它崩潰/上distanceFromLocation部分給出EXC_BAD_ACCESS (code =EXC_ARM_DA_ALIGN, address=.....etc....)。我附上了崩潰日誌的截圖。

Crash Log

什麼可能導致此,如何解決呢?

回答

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]; 
} 

,我認爲你有至少一個NUL運行distanceFromLocation: L參數或對象。

+0

是的我記錄了location1 && location2非零如果方法,它不顯示...我擺脫了!錯誤,只是作出了錯誤需要 – MCKapur 2012-04-13 11:04:15

+0

現在沒有崩潰順便說一句..... – MCKapur 2012-04-13 11:06:59

+0

但文本沒有設置 也我修正了: – MCKapur 2012-04-13 11:07:15