向MKMapView添加注释时发生EXC_BAD_ACCESS错误

时间:2016-04-03 03:31:47

标签: ios objective-c mkmapview mkannotation

我想使用lat和lon显示注释,lat和lon来自JSON(nearbyStop),我将经度和纬度存储到两个NSArrays中。当我尝试在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

2 个答案:

答案 0 :(得分:1)

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

Queue<String> yettoracequeue = new LinkedList<String>();
   // ^^^^^^

可以缩减为

@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..

因为没有必要为属性声明ivars。

和这些

@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...

可以删除,因为您不需要合成属性,除非它们来自@synthesize phone; @synthesize time; @synthesize coordinateL; title subtitle MKAnnotation符合的MKPlacemark协议。

coordinateL已经定义MKPlacemark

时,我有点混淆你为何宣布coordinate

另一个可能的问题是你没有在init方法中调用超类。

所以你可能在你的init方法中都需要这个......

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

答案 1 :(得分:0)

我怀疑这些行是问题所在:

NSString *longi = stop[@"result"][@"lon"];

NSString *longi = stop[@"result"][@"lat"];

因为变量stop是nil,或者字典stop[@"result"]是nil。尝试从nil字典中获取值会导致应用程序崩溃,但添加nil检查字典(在这种情况下为stopstop[@"result"])应该防止它崩溃,但是还让你有机会对不正确的字典状态进行一些错误处理。