自定义地图注释 iPhone 中的警告

发布于 2024-11-05 10:17:42 字数 221 浏览 1 评论 0原文

我正在 iPhone 中使用自定义地图注释类进行地图视图。每当我从导航栏堆栈中弹出地图视图时,我通常会在控制台中看到一些警告。

MapAnnotation 已被释放,而键值观察者仍向其注册。观察信息被泄露,甚至可能被错误地附加到其他物体上。在 NSKVODallocateBreak 上设置断点以在调试器中停止。这是当前的观察信息:

我没有在代码中使用 KVO,因此无法理解为什么我收到这些警告

I am using a custom map annotation class for map view in iPhone. Whenever I pop my map view from navigation bar stack I usually see some warnings in console.

MapAnnotation was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

I am not using KVO in my code hence not able to understand why I am receiving these warnings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

焚却相思 2024-11-12 10:17:42

纬度和经度具有不同的界限:纬度

  • 为 (-90, 90),长度为
  • (-180, 180)

传递超出这些界限的值将导致自定义类被释放,从而导致您收到错误。确保您传递了正确的纬度和经度值。

如果苹果为此传递一个边界错误而不是早期发布错误,那就太好了。这将为我节省大约 5 个小时的时间

Latitude and Longitude have differing bounds:

  • (-90, 90) for Lat
  • (-180, 180) for Long

Passing a value outside of those bounds will cause the custom class to be deallocated and therefore giving you the error that you're receiving. Make sure that you are passing the correct values for both Latitude and Longitude.

It would be really nice if Apple passed a bounding error for this instead of an early release error. That would've saved me roughly 5 hours worth of time

╄→承喏 2024-11-12 10:17:42

我遇到了与您相同的错误:

An instance 0xa975400 of class xxxxxx was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0xa980eb0> (
<NSKeyValueObservance 0xa980e70: Observer: 0xa94f8a0, Key path: coordinate, Options: <New: NO, Old: NO, Prior: YES> Context: 0x0, Property: 0xa980ef0>

正如此处所指出的,这是因为我向 MKMapView 添加了带有无效坐标的 MKAnnotation。

我的解决方案是创建一个函数来检查坐标是否有效。

Place+MKAnnotation.m

我创建了 Place 类的一个类别,

#import "Place.h"
#import <MapKit/MapKit.h>

@interface Place (MKAnnotation) <MKAnnotation>
- (BOOL)coordinateIsValid;
...
@end

Place+MKAnnotation.m

@implementation Place (MKAnnotation)

- (BOOL)coordinateIsValid
{
    return CLLocationCoordinate2DIsValid(CLLocationCoordinate2DMake([self.latitude doubleValue],[self.longitude doubleValue]));

}
...
@end

仅在坐标有效的情况下在 ViewController 中添加注释。

if([p coordinateIsValid]) {
    [self.mapView addAnnotation:p];
} 

I was getting the same error as you:

An instance 0xa975400 of class xxxxxx was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0xa980eb0> (
<NSKeyValueObservance 0xa980e70: Observer: 0xa94f8a0, Key path: coordinate, Options: <New: NO, Old: NO, Prior: YES> Context: 0x0, Property: 0xa980ef0>

As pointed here it was caused because I was adding a MKAnnotation with an invalid coordinate to a MKMapView.

My solution was to create a function to check the coordinate valid.

Place+MKAnnotation.m

I created a category of my Place class and

#import "Place.h"
#import <MapKit/MapKit.h>

@interface Place (MKAnnotation) <MKAnnotation>
- (BOOL)coordinateIsValid;
...
@end

Place+MKAnnotation.m

@implementation Place (MKAnnotation)

- (BOOL)coordinateIsValid
{
    return CLLocationCoordinate2DIsValid(CLLocationCoordinate2DMake([self.latitude doubleValue],[self.longitude doubleValue]));

}
...
@end

I only add the annotation in my ViewController if the coordinate is valid.

if([p coordinateIsValid]) {
    [self.mapView addAnnotation:p];
} 
燃情 2024-11-12 10:17:42

修好了。我在注释中使用了错误的纬度和经度对,我已经进行了相同的更改,现在一切似乎都很完美,警告也消失了。

Fixed it. I was using a wrong pair of latitude and longitude in annotations, I have changed the same and now everything seems to be perfect and warning has been disappeared as well.

漆黑的白昼 2024-11-12 10:17:42

在将注释添加到 MapView 之前是否会自动释放注释?

如果是这样,请尝试分配它,将其添加到 MapView,然后释放它。

Are you autoreleasing your annotation before adding it to the MapView?

If so, try just allocating it, add it to the MapView, then release it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文