2015-05-09 5 views
0

Здравствуйте, ниже, это код для CCHMapClusterController. Im пытается переписать эту функцию Swift, но я получаю эту ошибку:Переменная аннотация, используемая до инициализации

«Variable аннотации использовала до инициализируются»

на линии: clusterAnnotation = аннотации

Я понятия не имею, в Objective C, может ли кто-нибудь проверить, правильно ли я сделал последние несколько строк?

Objective C:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView; 

    if ([annotation isKindOfClass:CCHMapClusterAnnotation.class]) { 
    static NSString *identifier = @"clusterAnnotation"; 

    ClusterAnnotationView *clusterAnnotationView = (ClusterAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (clusterAnnotationView) { 
     clusterAnnotationView.annotation = annotation; 
    } else { 
     clusterAnnotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
     clusterAnnotationView.canShowCallout = YES; 
    } 

    CCHMapClusterAnnotation *clusterAnnotation = (CCHMapClusterAnnotation *)annotation; 
    clusterAnnotationView.count = clusterAnnotation.annotations.count; 
    clusterAnnotationView.blue = (clusterAnnotation.mapClusterController == self.mapClusterControllerBlue); 
    clusterAnnotationView.uniqueLocation = clusterAnnotation.isUniqueLocation; 
    annotationView = clusterAnnotationView; 
} 

return annotationView; 
} 

Swift Code:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if annotation is MKUserLocation { 
     // return nil so map view draws "blue dot" for standard user location 
     return nil 
    } 

    var annotationView : MKAnnotationView? 


    if annotation is CCHMapClusterAnnotation { 
     // let a : clusterAnnotationView = annotation as clusterAnnotationView 

     let identifier: NSString = "clusterAnnotation" 
     var clusterAnnotationView = (mapView.dequeueReusableAnnotationViewWithIdentifier(identifier as String)) as? ClusterAnnotationView! 

     if (clusterAnnotationView != nil) { 
      clusterAnnotationView!.annotation = annotation 
     } else { 
      clusterAnnotationView = ClusterAnnotationView(annotation: annotation, reuseIdentifier: "clusterAnnotation") 
      clusterAnnotationView!.canShowCallout = true 
     } 

     var clusterAnnotation : CCHMapClusterAnnotation; 
     var annotation : CCHMapClusterAnnotation 
     clusterAnnotation = annotation 
     clusterAnnotationView.count = clusterAnnotation.annotations.count 
     clusterAnnotationView!.blue = (clusterAnnotation.mapClusterController == self.mapClusterControllerBlue); 
     clusterAnnotationView!.uniqueLocation = clusterAnnotation.isUniqueLocation(); 
     annotationView = clusterAnnotationView 

    } 

ответ

1

С этими строками:

var clusterAnnotation : CCHMapClusterAnnotation; 
var annotation : CCHMapClusterAnnotation 
clusterAnnotation = annotation 

две переменные объявлены, но не инициализированы, так что вы получите это предупреждение.

Кроме того, он также объявляет новую локальную переменную annotation, имеющую то же имя, что и существующий параметр annotation. Возможно, вы получите предупреждение или ошибку.

Если вы пытаетесь преобразовать эту Objective-C линия:

CCHMapClusterAnnotation *clusterAnnotation = 
    (CCHMapClusterAnnotation *)annotation; 

, который литья параметр annotation как CCHMapClusterAnnotation, то возможно Swift версия:

let clusterAnnotation = annotation as? CCHMapClusterAnnotation 


Однако вы также можете совместить проверку класса аннотации, которую вы имеете выше (if annotation is ...) с этим литым назначением:

if let clusterAnnotation = annotation as? CCHMapClusterAnnotation { 
    ... 
} 
Смежные вопросы