MKAnotation - 不能将typ x的值赋给MKAnnotation类型

时间:2017-02-15 17:50:51

标签: swift xcode mkannotation

我有一张注释工作正常的地图。我想要一个segmentedcontrol来改变注释。

此代码

annotations = getMapAnnotations()
// Add mappoints to Map
mapInfView.addAnnotations(annotations )

在ViewDidLoad中工作但我在更改plist文件以获取数据后尝试实现它。 我不明白为什么我不能将类型[' Stands']的值分配给tupe MKAnnotation?当它在segmentedcontrol代码中而不在viewdidload中时。 有没有更好的方法来更改注释?

getannotations()

 func getMapAnnotations() -> [Stands] {
    var annotations:Array = [Stands]()
    print("ANNOTATIONS")
    //load plist file
    var stands: NSArray?
    print("(FILE \(iFile)")
    if let path = Bundle.main.path(forResource:  iFile, ofType: "plist")     {
      stands = NSArray(contentsOfFile: path)
      print(path)
       print(stands)  
    }

//iterate and create annotations
if let items = stands {
  for item in items {
    let lat = (item as AnyObject).value(forKey: "lat") as! Double
    let long = (item as AnyObject).value(forKey: "long")as! Double
    let annotation = Stands(latitude: lat, longitude: long)
    let tit = (item as AnyObject).value(forKey: "title") as! String
    let sub = (item as AnyObject).value(forKey: "subtitle") as! String

    annotation.title = tit   //"\(numb) \(tit)"
    annotation.subtitle = sub

    annotations.append(annotation)
  }
}
return annotations

}

看台等级

import Foundation

import MapKit

class Stands: NSObject, MKAnnotation {
  var title: String?
  var subtitle: String?
  var no: Int?
  var latitude: Double
  var longitude:Double

  var coordinate: CLLocationCoordinate2D {
    return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  }

  init(latitude: Double, longitude: Double) {
    self.latitude = latitude
    self.longitude = longitude
  }
}

IBAction代码

@IBAction func annotType(_ sender: Any) {
   let infoChoice = AnnotType(rawValue:  infoSegmentController.selectedSegmentIndex)
    switch (infoChoice!) {
    case .WC:
      iFile = "wc"
    case .Restaurant:
      iFile = "restaurant"
    case .ATM:
      iFile = "atm"
    case .Museums:
      print("Museums")
      iFile = "museums"

}
print("END")
annotations = getMapAnnotations()
// Add mappoints to Map
mapInfView.addAnnotations(annotations )

}

1 个答案:

答案 0 :(得分:0)

亚光是对的。 在ViewDid加载中我有

template<typename... Attr>
double get_probability(const MeanStdDevVec& v, const Student& s, Attr... attr) {

    assert(v.size() == sizeof...(Attr));
    auto probs = index_pack_to_array<double>(
        [&] (std::size_t i, auto&& a) -> double { 
            return // ... (get probability from v[i], s, and a)
        },
        std::forward<Attr>(attr)...);

    return std::accumulate(probs.begin(), probs.end(), 1.0,
        [] (double p1, double p2) { return p1 * p2; });
}

in @IBAction func annotType()

我有

var annotations = getMapAnnotations()

添加var以再次声明它解决了问题。

在类声明之后,以及我有的许多其他变量声明

annotations = getMapAnnotations()

这似乎没有任何意义。我只是评论了它,应用程序运行正常。