0

抱歉,这可能是一个菜鸟问题,但我正在与 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 模拟器上运行的谢谢。

4

3 回答 3

1

currLoc.coordinate.latitude 是双精度类型...如果 %g 不起作用,您可以使用 %f

NSLog(@"Current location %f",currLoc.coordinate.latitude);
于 2010-05-06T06:21:18.263 回答
0

currLoc可能为not Nil check返回TRUE。但是可能有机会释放对象(悬空指针),这将通过 nil 检查条件。这可能是你的问题。

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

于 2010-05-06T07:03:21.810 回答
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);
于 2010-05-06T07:33:09.697 回答