gpt4 book ai didi

objective-c - MKMapKit-遍历MKA注释时为EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-12-01 19:27:23 28 4
gpt4 key购买 nike

我已经被困在这个EXC_BAD_ACCESS错误2天了。我有一个reloadAnnotations方法,可以在添加新注释之前删除所有注释。在删除注释之前,此方法应检查新集合是否包含相同的位置,以便不会将其删除并重新添加。但是,一旦我尝试找出当前的注释标题,就会收到此错误Thread 1: Program received signal: "EXC_BAD_ACCESS"
当我在调试器中查看批注时,标题属性会显示“无效摘要”。这一定是由于未保留值而引起的,但是我已经尝试了所有方法并且无法弄清楚。

为什么我不能将注释标题记录到NSLog?

并且为什么我不能将每个标题和座标与其他对象进行比较?

BrowseController.m

-(void)reloadAnnotations
{
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10];
for (id annotation in _mapView.annotations) {
if (annotation != _mapView.userLocation) {
//ParkAnnotation *pa = (ParkAnnotation *)annotation;
ParkAnnotation *pa = annotation;
NSLog(@"pa.title %@", pa.title); // Thread 1: Program received signal: "EXC_BAD_ACCESS"
[toRemove addObject:annotation];
}
}
// DON'T REMOVE IT IF IT'S ALREADY ON THE MAP!!!!!!
for(RKLocation *loc in locations)
{
CLLocationCoordinate2D location;
location.latitude = (double)[loc.lat doubleValue];
location.longitude = (double)[loc.lng doubleValue];
ParkAnnotation *parkAnnotation = [[ParkAnnotation alloc] initWithTitle:loc.name andCoordinate:location];
[_mapView addAnnotation:parkAnnotation];
}
[_mapView removeAnnotations:toRemove];
}



- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"BrowseViewController map viewForAnnotation");
MKPinAnnotationView *pin = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier: @"anIdentifier"];

if (pin == nil){
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier: @"anIdentifier"] autorelease];

pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
pin.canShowCallout = YES;
}
else{
pin.annotation = annotation;
}
return pin;
}

ParkAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface ParkAnnotation : NSObject <MKAnnotation> {

NSString *title;
CLLocationCoordinate2D coordinate;

}


@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;

@end

ParkAnnotation.m(编辑:请参见下面的Wolfgangs评论)
#import "ParkAnnotation.h"
@implementation ParkAnnotation
@synthesize title, coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
self = [super init];
if (self) {
title = ttl;
coordinate = c2d;
}
return self;
}
- (void)dealloc {
[title release];
[super dealloc];
}
@end

最佳答案

ParkAnnotation.m 中的初始化程序未遵循ObjC约定编写。永远不会设置self变量,指定的类初始化器应遵循以下模式:

- (id)init 
{
self = [super init];
if (self)
{
/* custom initialization here ... */
}
return self;
}

由于未设置self,因此调用方中使用的访问器方法将失败;当尝试从另一个类访问该对象内部的属性时,该容器对象(在被自己引用的 ParkAnnotation.m 内部)将为nil或某些虚假值。

关于objective-c - MKMapKit-遍历MKA注释时为EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6221893/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com