CLLocation不是nil但是尝试将其打印出来会抛出EXC_BAD_ACCESS

时间:2010-05-06 06:17:04

标签: iphone gps exc-bad-access cllocation core-location

对不起,这可能是一个菜鸟问题,但我正在使用CoreLocation而且这很难过。

我正在使用此站点上推荐的单例查找currentLocation,当我获取currentLocation对象时,它返回true为非零检查。但是,当我尝试打印其描述时,它会抛出EXC_BAD_ACCESS。

//WORKS Current location 8.6602e-290
NSLog(@"Current location %g",currLoc);

//DOESN'T WORK
NSLog(@"Current location %@",[currLoc description]);

//DOESN'T WORK - Is this causing the description to fail as well?
NSLog(@"Current location %g",currLoc.coordinate.latitude);

为什么我能看到第一个而不是其他的东西?顺便说一句,这是在3.1.2模拟器上运行谢谢。

3 个答案:

答案 0 :(得分:1)

currLoc.coordinate.latitude类型为double ...如果%g无效,您可以使用%f

NSLog(@"Current location %f",currLoc.coordinate.latitude);

答案 1 :(得分:0)

currLoc 可能会为返回 TRUE 而非检查。但是可能存在释放对象(悬空指针)的机会,这将通过零检查条件。这可能是你的问题。

不要直接使用 currLoc成员,而是使用Accessor for currLoc 。这将解决您的问题:)

答案 2 :(得分:0)

// malformed: %g format specifier expects type
// double, you're passing an objc object.
// sending arguments through (...) with the
// invalid types/widths is a near certain way
// to crash an app, and the data will be useless
// to the function called (NSLog in this case)
NSLog(@"Current location %g",currLoc);

// try running the app with zombies enabled.
// in short, enabling zombies (NSZombiesEnabled)
// is a debugging facility to determine whether
// you use an objc object which would have been freed.
NSLog(@"Current location %@",[currLoc description]);

// as long as currLoc is a pointer to a valid (non-freed)
// objc object, then this should be fine. if it is not valid
NSLog(@"Current location %g",currLoc.coordinate.latitude);