1

その情報で注釈を使用しようとするNSMutableArrayと、マップ座標がありますが、このエラーが行 annotation.coordinate で発生しています。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;
}
4

1 に答える 1

0

あなたのエラーは、辞書ではなく実際には配列である何かに対して辞書の添字構文を使用しようとしていることを示していますdictionary[key](その構文を使用すると、メソッドが呼び出されます)。objectForKeyedSubscript次の 2 つのことを行う必要があります。

  1. まず、問題の原因となっているコード行を正確に確認します。あなたが私たちと共有することを選択したコードに基づいて、forループ内のそのコードからのものであるとあなたは疑ってcoordinate​​いると思いますtitle

    ただし、このコードが問題の原因であることを確認する必要があります (コード内の他の何かではありません)。これは、例外ブレークポイントを使用するか、このルーチンを 1 ステップ実行することで実行できます。

    オブジェクトの内容を(デバッガーで、またはNSLogステートメントを挿入して)調べる必要がありdictionaryます。あなたのエラーは、それが実際には配列であり、辞書ではないことを示唆しています。

  2. コードのどの行が問題を引き起こしているかを確認したら、一歩下がって、オブジェクト ポイントが辞書ではなく配列になった理由を特定する必要があります。(a) このメソッドが呼び出された方法 (たとえば、間違ったパラメーターが渡された)。または(b)辞書の元の配列がどのように作成されたか。

于 2013-10-05T02:33:14.950 に答える