带注释的IOS 7错误

时间:2013-10-05 02:09:07

标签: ios annotations nsmutablearray

当我尝试使用带有该信息的注释时,我有NSMutableArray地图坐标我在线上注释时遇到此错误。它正在IOS 5和6上工作:

  

- [__ 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;
}

1 个答案:

答案 0 :(得分:0)

您的错误告诉您,您尝试使用dictionary[key]的字典下标语法(当您使用该语法时,调用objectForKeyedSubscript方法)对非字典的内容,但实际上是一个数组。你应该做两件事:

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

    但是您确实应该确认此代码导致了问题(而不是代码中的其他内容)。您可以使用exception breakpoint或单步执行此例程来执行此操作。

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

  2. 一旦确认哪一行代码导致问题,您就需要退后一步,确定对象点如何最终作为数组而不是字典。它可以是(a)如何调用此方法(例如,错误的参数传递);或(b)如何创建原始字典数组。