ios - 给MKMapView添加注解时出现EXC_BAD_ACCESS错误

标签 ios objective-c mkmapview mkannotation

我想使用纬度和经度显示注释,纬度和经度来自 JSON(nearbyStop),我将经度和纬度存储到两个 NSArray 中。当我尝试在 for 循环中向 mapview 添加注释时,出现 EXC_BAD_ACCESS 错误 enter image description here 有帮助吗?

    NSMutableArray *coordinateLongi = [[NSMutableArray alloc]init];
for (NSDictionary *stop in nearbyStop) {
    NSString *longi = stop[@"result"][@"lon"];
    if(longi == nil)
    {
        NSLog(@"there is no data");
    }
    else
    {
        [coordinateLongi addObject:longi];
    }
}

NSMutableArray *coordinateLatit = [[NSMutableArray alloc]init];
for (NSDictionary *stop in nearbyStop) {
    NSString *latit = stop[@"result"][@"lat"];
    if(latit == nil)
    {
        NSLog(@"there is no data");
    }
    else
    {
        [coordinateLatit addObject:latit];
    }
}

for(int i = 0; i<coordinateLongi.count;i++)
{
    CLLocationCoordinate2D coord;
    coord.latitude = [[NSString stringWithFormat:@"%@",[coordinateLongi objectAtIndex:i]] floatValue];
    for(int j = 0; j<coordinateLatit.count;j++)
    {
        coord.longitude = [[NSString stringWithFormat:@"%@",[coordinateLatit objectAtIndex:i]] floatValue];
    }


    CustomAnnotation *annotObj = [[CustomAnnotation alloc] initWithCoordinate:coord title:@"title" ];
    [map addAnnotation:annotObj];//Error occurs here.
}

这是我的 customAnnotation.h

 #import <MapKit/MapKit.h>

@interface CustomAnnotation : MKPlacemark
{
    CLLocationCoordinate2D coordinateL;
    NSString *title;
    NSString *subtitle;
    NSString *time;
}

@property (nonatomic)CLLocationCoordinate2D coordinateL;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *time;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c  title:(NSString *) t  subTitle:(NSString *)timed time:(NSString *)tim;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit;



@end

和 customAnnotation.m

    #import "CustomAnnotation.h"

@implementation CustomAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize phone;
@synthesize time;
@synthesize coordinateL;


-(id)initWithCoordinate:(CLLocationCoordinate2D) c  title:(NSString *) t  subTitle:(NSString *)timed time:(NSString *)tim
{
    self.coordinateL=c;
    self.time=tim;
    self.subtitle=timed;
    self.title=t;
    return self;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit
{
    self.coordinateL=c;
    self.title=tit;
    return self;
}

@end

最佳答案

这可能不是原因,但这...

@interface CustomAnnotation : MKPlacemark
{
    CLLocationCoordinate2D coordinateL;
    NSString *title;
    NSString *subtitle;
    NSString *time;
}

@property (nonatomic)CLLocationCoordinate2D coordinateL;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *time;

etc..

可以减少到

@interface CustomAnnotation : MKPlacemark

@property (nonatomic)CLLocationCoordinate2D coordinateL;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *time;

etc...

因为不需要为属性声明 ivars。

还有这些

@synthesize phone;
@synthesize time;
@synthesize coordinateL;

可以删除,因为你不需要综合属性,除非它们来自 titlesubtitleMKAnnotation 中执行的协议(protocol),其中 MKPlacemark 符合。

MKPlacemark 已经定义了 coordinate 时,为什么还要声明 coordinateL 我有点困惑

另一个可能的问题是您没有在 init 方法中调用父类(super class)。

所以你可能在你的两个初始化方法中都需要这个...

-(id)initWithCoordinate:(CLLocationCoordinate2D)coord title:(NSString *)title
{
    self = [super initWithCoordinate:coord addressDictionary:nil];
    if(self) {
      self.coordinateL = coord;
      self.title = title;
    }
    return self;
}

关于ios - 给MKMapView添加注解时出现EXC_BAD_ACCESS错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36381332/

相关文章:

ios - 如何测量 iOS 的 Objective-C 中两行代码之间花费的时间?

ios - iOS 10中的AVAssetExportSession不适用于iPhone 7

iphone - 检查用户是否遵循路线(iphone)

objective-c 二进制表达式的无效操作数 double 到 double

iOS:我的问题是与其他人询问的终止模式下的背景位置跟踪有什么不同

iphone - -[__NSArrayM objectAtIndex :]: index 4294967295 beyond bounds for empty array with arc4random

ios - 带有折线或注释的 MapView 在缩放时丢失图 block

ios - 为什么导入 <libxml/tree.h> 时出现 GDataXML 错误

objective-c - 多个私有(private) repo cocoapods

ios - MKMapView 有时会缩放到用户位置有时会缩放到不知名的地方