0

我的 JSON 响应有问题。由于某种原因,每次我运行程序时都会出现线程错误,也许它与错误有关..?我正在尝试获取 JSON 响应并根据 JSON 请求使用一组标记填充地图。

NSURLConnection
     sendAsynchronousRequest:urlRequest
     queue:queue
     completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
         if([data length] > 0 &&
            error == nil){
             NSData *jsonData = [NSData dataWithContentsOfURL:url];

             if (jsonData != nil){
                 NSError *error = nil;

                 self.results = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];


                 if(error == nil)

                     self.locations = _results;
                 for (NSDictionary *locations1 in self.locations){
                     CLLocationCoordinate2D annotationCoordinate =
                     CLLocationCoordinate2DMake([locations1[@"latitude"] doubleValue], [locations1[@"longitude"] doubleValue]);
                     Annotation *annotation2 = [[Annotation alloc] init];
                     annotation2.coordinate = annotationCoordinate;
                     annotation2.title = locations1[@"name"];
                     annotation2.subtitle = nil;
                     [self.mapView addAnnotation:annotation2];
    **ERROR: sending 'Annotation *__strong' to parameter of incompatible type 'id<MKAnnotation>'
                 }

我不确定我的代表会出现什么问题?有任何想法吗?

4

1 回答 1

0

问题无疑Annotation是没有定义为符合MKAnnotation协议。它应该被定义为:

@interface Annotation : NSObject <MKAnnotation>
// ...
@end

或者您可以替换AnnotationMKPointAnnotation,代码也应该可以工作。(你真的需要自己的注解类吗?)

最后,不相关的是,您dataWithContentsOfURL在完成块中执行,但这是不必要的,因为sendAsynchronousRequest已经为您检索数据并将其传递给您的completionHandler.

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
     if ([data length] > 0 && error == nil) {
         NSError *error = nil;

         self.results = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

         if (error == nil)
             self.locations = _results;

         for (NSDictionary *location in self.locations) {
             CLLocationCoordinate2D annotationCoordinate = CLLocationCoordinate2DMake([location[@"latitude"] doubleValue], [location[@"longitude"] doubleValue]);
             Annotation *annotation = [[Annotation alloc] init];
             annotation.coordinate = annotationCoordinate;
             annotation.title = location[@"name"];
             annotation.subtitle = nil;
             [self.mapView addAnnotation:annotation];
         }
     }
 }];
于 2013-09-09T05:44:25.670 回答