CoreLocation在设备上崩溃但在模拟器上没有崩溃

时间:2010-09-11 15:46:08

标签: iphone core-location

继续获取EXC_BAD_ACCESS。 Ran NSZombieEnabled并没有提出任何建议。

在模拟器控制台中:

 2010-09-11 23:39:56.876 [19072:207] 1.309789, lat, 103.772196, lon

在设备控制台中:

EXC_BAD_ACCESS

代码行:

 NSLog(@"%f, lat, %f, lon",latitudeString,longitudeString);

CLManager在做什么:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 NSLog(@"Entering mlocationmanager did updatetolocation");
    latitudeString = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];

    longitudeString = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];

  NSLog(@"lat %@ long %@", latitudeString, longitudeString);
 NSLog(@"%@",newLocation.description);
 if (latitudeString.length >0 && longitudeString.length > 0){
  NSLog(@"Yes both are more than 0");
  locationIsReady = YES;

 }
 [self.tableView reloadData]; 
}

这里发生了什么?这两个变量都在我的头文件中,并且符合CLLocationManagerDelegate。已经工作了几个小时,但没有运气。希望你们能帮忙。

NSString *latitudeString;
 NSString *longitudeString;

3 个答案:

答案 0 :(得分:2)

您确实无需将newLocation.coordinate.longitude转换为NSString即可将其打印出来。只需在格式字符串中使用%f即可打印它们。

另外,当你写:

if (latitudeString.length >0 && longitudeString.length > 0)

您正在比较字符串的长度始终大于零。因为经度或纬度为零,转换后的字符串将为@"0.0",其长度为3。我怀疑你真正想要的是:

if (newLocation.coordinate.latitude != 0.0
    && newLocation.coordinate.longitude != 0.0)

答案 1 :(得分:1)

代码行确实应该是你的罪魁祸首:

NSLog(@"%f, lat, %f, lon",latitudeString,longitudeString);

Objective-C对象的正确NSLog格式参数为%@%f用于浮点数。

答案 2 :(得分:1)

最后在很长一段时间后修复它。

花了一些时间才意识到NSXMLParser实际上覆盖了我的一些变量(我不知道为什么)。所以最后,我在解析XML后调用了CLLocationManager。问题解决了。

相关问题