2013-10-05 45 views
1

我有地图的坐标NSMutableArray当我尝试使用注释与信息我正在上线annotation.coordinate这个错误,它正在对iOS 5和6:IOS 7误差与注释

- [__ NSCFArray objectForKeyedSubscript:]:无法识别的选择发送到实例

我有这样的代码:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]; 

NSError *error; 
NSArray *array = [NSJSONSerialization JSONObjectWithData:data 
               options:0 
                error:&error]; 


NSString *value = [array valueForKey:@"cortesMap"]; 
NSMutableArray *arr = [[NSMutableArray alloc] init]; 
[arr addObject:value]; 
if (error) 
{ 
    NSLog(@"%s: error=%@", __FUNCTION__, error); 
    return; 
} 

for (NSDictionary *dictionary in arr) 
{ 
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
    annotation.coordinate = CLLocationCoordinate2DMake([dictionary[@"latitude"] doubleValue], [dictionary[@"longitude"] doubleValue]); 
    annotation.title = dictionary[@"type"]; 
    annotation.subtitle = dictionary[@"description"]; 
    [self.mapView addAnnotation:annotation]; 

} 

这里是viewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView= nil; 
    if (![annotation isKindOfClass:[MKUserLocation class]]) 
    { 
     static NSString *annotationViewId = @"annotationViewId"; 
     annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewId]; 
     if (annotationView == nil) 
     { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewId]; 

      annotationView.canShowCallout = YES; 
      annotationView.image = [UIImage imageNamed:@"pin.png"] 
     } 
     else 
     { 
      annotationView.annotation = annotation; 
     } 
    } 
    return annotationView; 
} 

回答

0

你的错误是告诉你,你正在尝试使用的dictionary[key]字典下标语法上的东西(它,当您使用语法,调用objectForKeyedSubscript法),是不是一本字典,但实际上是一个数组。你应该做两件事:

  1. 首先,确切地确认哪一行代码导致问题。我假设,根据您选择与我们分享的代码,您怀疑它来自您的for循环中的代码,其中您检索的是coordinate,title等。

    但是你真的应该确认这段代码导致了问题(而不是代码中的其他内容)。您可以通过exception breakpoint或单步执行此例程来完成此操作。

    您应该检查(在调试器中或通过插入NSLog语句)dictionary对象的内容。你的错误表明它实际上是一个数组,而不是一本字典。

  2. 确认哪一行代码导致问题后,您需要退后一步,确定对象点是如何以数组而不是字典的形式结束的。它可以是(a)如何调用这个方法(例如传递了错误的参数);或(b)如何创建原始字典阵列。

+0

带有错误的行是annotation.coordinate与经度和纬度,我用nslog检查数组,并且它具有我想要的正确信息。 – dastjuso

+0

@dastjuso'dictionary'对象真的是'NSDictionary'吗?没有。 – Rob

+0

我为字典添加了一行新的代码,但仍然出现相同的错误。 – dastjuso