Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Mapbox iOS SDK中创建开关语句_Ios_Swift_Mapbox - Fatal编程技术网

在Mapbox iOS SDK中创建开关语句

在Mapbox iOS SDK中创建开关语句,ios,swift,mapbox,Ios,Swift,Mapbox,我正在尝试为Mapbox iOS中的leftCalloutAccessoryViewForAnnotation创建一个switch语句。我尝试了各种方法,包括创建CustomPointAnnotation类和重用标识符,但都无法使其工作 最后,我创建了您在下面看到的内容。这不是我想使用的那种代码。任何意见都将不胜感激 func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnn

我正在尝试为
Mapbox iOS
中的
leftCalloutAccessoryViewForAnnotation
创建一个switch语句。我尝试了各种方法,包括创建
CustomPointAnnotation
类和重用标识符,但都无法使其工作

最后,我创建了您在下面看到的内容。这不是我想使用的那种代码。任何意见都将不胜感激

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? { 

  if (annotation.subtitle! == "Name") { 
    let imageView = UIImageView(image: UIImage(named: "imageName")!)
    self.view.addSubview(imageView)
    return imageView
  }

  if (annotation.subtitle! == "Name2"){
    let imageView = UIImageView(image: UIImage(named: "imageName2")!)
    self.view.addSubview(imageView)
    return imageView
  }

  if (annotation.subtitle! == "Name3"){
    let imageView = UIImageView(image: UIImage(named: "imageName3")!)
    self.view.addSubview(imageView)
    return imageView
  }
  return nil
}
注释

for location in locations {let annotation = MGLPointAnnotation()
        let coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude);
        annotation.coordinate = coordinate
        annotation.title = location.title
        annotation.subtitle = location.subtitle

        annotations.append(annotation)
        mapView.delegate = self
        mapView.addAnnotations(annotations)
这应该做到:

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {

    var imageView = UIImageView()
    imageView.frame = ... // Set the frame for the imageView.

    // Optional binding to either create non-optional value subtitle or escape the statement.
    if let subtitle = annotation.subtitle {
        // subtitle is a non-optional value, cases must also be non-optional.
        switch subtitle {
        case "Name": imageView.image = UIImage(named: "imageName")
        case "Name2": imageView.image = UIImage(named: "imageName2")
        case "Name3": imageView.image = UIImage(named: "imageName3")
        default: return nil
        }

        self.view.addSubview(imageView)
        return imageView
    }

    return nil
}
如果不想使用可选绑定,可以采用另一种方法:

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {

    var imageView = UIImageView()
    imageView.frame = ... // Set the frame for the imageView.

    // annotation.subtitle is an optional value, cases must also be optional.
    switch annotation.subtitle {
    case nil: return nil
    case "Name"?: imageView.image = UIImage(named: "imageName")
    case "Name2"?: imageView.image = UIImage(named: "imageName2")
    case "Name3"?: imageView.image = UIImage(named: "imageName3")
    default: return nil
    }

    self.view.addSubview(imageView)
    return imageView
}

根据字幕切换不是一个好方法。您的逻辑是基于UI层的,这是向后的。除了令人困惑的体系结构之外,当您本地化应用程序时,它还会崩溃

您应该子类化
MGLPointAnnotation
,并添加一个属性来指示点的类型。我会这样做:

enum PointType {
    case FirstPointType
    case SecondPointType
    case ThirdPointType

    var imageName: String {
        get {
            /*
             You can implement whatever logic you'd like here, but if
             you follow the pattern of "FirstPointType.png" for your
             images, defaulting to the case name is probably easiest.
             */
            return String(self)
        }
    }

    var image: UIImage? {
        get {
            return UIImage(named: imageName())
        }
    }

}

class CustomAnnotation: MGLAnnotation {
    var pointType: PointType
}

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? { 

    guard let annotation = annotation as? CustomAnnotation else { return nil }

    let imageView = UIImageView(image: annotation.pointType.image)
    self.view.addSubview(imageView)
    return imageView
}

在实现上述方法后,我不断收到以下错误:“String类型的表达式模式无法与String类型的值匹配?”我已编辑了原始问题,以包含用于在Mapbox中填充地图的注释信息。如果您遇到该错误,则未正确实现该错误。出现此错误的原因是
switch
语句无法将可选值与非可选值进行比较。在上面的回答中,
annotation.subtitle
的值通过与
if let
语句的可选绑定转换为非可选值或nil。