2017-10-10 72 views
2

我創建與地圖,並使用了iOS 11 MKMarkerAnnotationView「NSInvalidArgumentException」的使用MapKit

100-200 MKPointAnnotation簡單視圖控制器時,這是在控制器的viewDidLoad中

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.mapView.register(StationAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) 
    self.mapView.register(StationClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier) 

    locationDelegate.delegate = self 
    self.mapView.delegate = self 
    self.mapView.showsUserLocation = true 
    self.refreshData() 
    self.establishUserPosition() 
} 

然後,我從JSON(網絡對象)下載站,我全部添加到的MapView

func reloadViews(){ 
    if let network = network{ 
     for station in network.stations{ 
      let annotation = StationAnnotation(station: station) 
      annotations.append(annotation) // I add the annotations to an array to prevent them to be deallocated 
      mapView.addAnnotation(annotation) 
     } 
    } 
} 

這是我個人的註解

class StationAnnotation : MKPointAnnotation{ 
var station : Station? 
var tintColor : UIColor?{ 
    if self.station?.free_bikes ?? 0 > 0 { 
     return .green 
    }else{ 
     return .red 
    } 
} 

var glyphImage : UIImage?{ 
    if self.station?.extra.status == "online"{ 
     return UIImage(named: "Bicycle") 
    }else{ 
     return UIImage(named: "Ban") 
    } 
} 

override init() { 
    super.init() 
} 

convenience init(station : Station){ 
    self.init() 
    self.title = station.name 
    self.coordinate = CLLocationCoordinate2D(latitude: station.latitude, longitude: station.longitude) 
    self.station = station 
    if station.extra.status == "online"{ 
     self.subtitle = "Bikes: \(station.free_bikes) - Slots: \(station.empty_slots)" 
    }else{ 
     self.subtitle = station.extra.status 
    } 
} 
} 

我的意見海關

class StationAnnotationView : MKMarkerAnnotationView{ 

override var annotation: MKAnnotation? { 
    willSet { 
     if let annotation = newValue as? StationAnnotation{ 

      self.markerTintColor = annotation.tintColor 
      self.clusteringIdentifier = "station" 
      self.glyphImage = annotation.glyphImage 
     } 
    } 
} 
} 


class StationClusterView: MKAnnotationView { 

override init(annotation: MKAnnotation?, reuseIdentifier: String?) { 
    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

override var annotation: MKAnnotation? { 
    willSet { 
     if let cluster = newValue as? MKClusterAnnotation { 
      let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40)) 
      let count = cluster.memberAnnotations.count 
      let onlineCount = cluster.memberAnnotations.filter { member -> Bool in 
       return (member as! StationAnnotation).station?.extra.status == "online" 
       }.count 
      image = renderer.image { _ in 
       // Fill full circle with tricycle color 
       UIColor(named: "Forbidden")?.setFill() 
       UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill() 

       // Fill pie with unicycle color 
       UIColor(named: "Available")?.setFill() 
       let piePath = UIBezierPath() 
       piePath.addArc(withCenter: CGPoint(x: 20, y: 20), radius: 20, 
           startAngle: 0, endAngle: (CGFloat.pi * 2.0 * CGFloat(onlineCount))/CGFloat(count), 
           clockwise: true) 
       piePath.addLine(to: CGPoint(x: 20, y: 20)) 
       piePath.close() 
       piePath.fill() 

       // Fill inner circle with white color 
       UIColor.white.setFill() 
       UIBezierPath(ovalIn: CGRect(x: 8, y: 8, width: 24, height: 24)).fill() 

       // Finally draw count text vertically and horizontally centered 
       let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.black, 
            NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20)] 
       let text = "\(count)" 
       let size = text.size(withAttributes: attributes) 
       let rect = CGRect(x: 20 - size.width/2, y: 20 - size.height/2, width: size.width, height: size.height) 
       text.draw(in: rect, withAttributes: attributes) 
      } 
     } 
    } 
} 

} 

我不知道爲什麼應用程序,同時捏,縮放或平移,與SIGABRT信號偶們這個異常

*** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: key cannot be nil' 

我已經嘗試了各種調試系統,並且異常斷點的使用沒有幫助...您有什麼建議嗎?

+0

那爲零在'tintColor'是永遠不會返回> 0合併操作不能使用它這樣。我建議完全放棄可選項並將其默認值設置爲0. – brandonscript

+0

至於錯誤,您設置的某個字典或屬性的其中一個鍵爲零。您需要在每個步驟中設置斷點並檢查每個值。它可以是'attributes',它可以是'text.size()',或者其他地方。 – brandonscript

+0

「或其他地方完全」所以,不知道謝謝:) –

回答

0

大家好,我找到解決方案。 起初 - 當我們使用

mapView.register(AnyClass?, forAnnotationViewWithReuseIdentifier: String) 

mapView.dequeueReusableAnnotationView(withIdentifier: String) 

返回nil它s..t發生。

所以燙: 地址:

ViewController: UIViewController, MKMapViewDelegate 

添加

override func viewDidLoad() { 
     super.viewDidLoad() 
     mapView.delegate = self 

     mapView.register(MarkerPointView.self, forAnnotationViewWithReuseIdentifier: "marker") 
     mapView.register(ClusterView.self, forAnnotationViewWithReuseIdentifier: "cluster") 
} 

和finaly:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     if let marker = annotation as? MarkerAnnotation{ 
      var view = mapView.dequeueReusableAnnotationView(withIdentifier: "marker") as? MarkerPointView 
      if view == nil { 
//Very IMPORTANT 
       print("nil for Marker") 
       view = MarkerPointView(annotation: marker, reuseIdentifier: "marker") 
      } 
      return view 
     }else if let cluster = annotation as? MKClusterAnnotation{ 
      var view = mapView.dequeueReusableAnnotationView(withIdentifier: "cluster") as? ClusterView 
      if view == nil{ 
//Very IMPORTANT 
       print("nil for Cluster") 
       view = ClusterView(annotation: cluster, reuseIdentifier: "cluster") 
      } 
      return view 
     } 
     else{ 
      return nil 
     } 
    } 

希望這對別人的幫助,並在接下來的轉速蘋果修復它,因爲我們可以像他們在36:50在wwdc2017上所說的那樣使用它 - 我們不能刪除它!!!!! !

原來的職位上forums.developer.apple.com