objective-c - 初始化之前使用的变量注释

标签 objective-c swift

您好,下面是 CCHMapClusterController 的代码。我尝试将此函数重写为 Swift,但我不断收到此错误:

“初始化之前使用的变量注释”

上线: 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 代码:

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

    }

最佳答案

用这些行:

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 comment is ...)与此强制转换赋值结合起来:

if let clusterAnnotation = annotation as? CCHMapClusterAnnotation {
    ...
}

关于objective-c - 初始化之前使用的变量注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141587/

相关文章:

c# - 如何在 MonoTouch 和 Objective-C 之间做出选择?

objective-c - block 文字是否应该保留引用的堆分配 block

swift - 如何为 macOS 菜单栏应用程序启用自动启动?

ios - 单击 UIPopoverPresentationController 中的按钮移动到 View Controller

ios - 从核心数据中删除重复对象

iphone - 如何在 Cocoa Touch 的标签中包含超链接?

iOS 8 快速应用程序,内容不居中

IOS swift - 如何在点击文本字段时打开新的 View Controller 后结束文本字段中的编辑

ios - 我可以将观察者添加到 GMSMapView() 的 "zoom"属性吗?

iphone - 如何检测哪个 3rd sdk 使用 UDID?