【问题标题】:Don't understand the crash log?看不懂崩溃日志?
【发布时间】:2012-04-13 10:00:22
【问题描述】:

我正在尝试了解崩溃日志。当我尝试执行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....)。我附上了崩溃日志的截图。

这可能是什么原因造成的,我该如何解决?

【问题讨论】:

    标签: iphone objective-c ios xcode crash


    【解决方案1】:

    我假设 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: 时至少带有一个 NULL 参数或对象。

    【讨论】:

    • 是的,我记录了 location1 && location2 非零 if 方法,但它没有显示...我摆脱了 !error 并为错误需要单独创建了一个 if(error) 语句
    • 但我也没有设置文本:
    • 我在原始字符串上犯了一个错误...我将 origin 设置为两者我已修复但仍然文本不起作用
    • 另一个更新....我刚刚登录以检查 location1 和 location2 在初始化后是否有效且非零,它说它们有效但在if string equalsToString 语句... CONFUSIN!!!!!!
    • 好的,我想通了...在实现之前将它们设为静态实例变量...谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多