[英]mapView.addAnnotation() says “unexpectedly found nil while unwrapping an Optional value”
I try to segue from the address of tableView cell to a mapView and show the pin on the map. 我尝试从tableView单元格的地址隔离到mapView并在地图上显示图钉。
I'm sure everything is not nil in my code. 我确定代码中的所有内容都不是零。
But Xcode says my oldValue
is nil
(in didSet{}
). 但是Xcode说我的
oldValue
是nil
(在didSet{}
)。 I don't know how to fix it. 我不知道该如何解决。
Following is my code: 以下是我的代码:
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView! {
didSet {
mapView.mapType = .Standard
mapView.delegate = self
}
}
var location:CLLocation? {
didSet {
clearWayPoints()
if location != nil {
println(location!.coordinate.longitude)
let coordinate = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let pin = MapPin(coordinate: coordinate, title: "Current", subtitle: "here")
setAnnotation(pin)
}
}
}
private func clearWayPoints() {
if mapView?.annotations != nil {
mapView.removeAnnotations(mapView.annotations as [MKAnnotation])
}
}
func setAnnotation(pin: MKAnnotation) {
mapView.addAnnotation(pin)
mapView.showAnnotations([pin], animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
var view = mapView.dequeueReusableAnnotationViewWithIdentifier(Constants.AnnotationViewReuseIdentifier)
if view == nil {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: Constants.AnnotationViewReuseIdentifier)
view.canShowCallout = true
} else {
view.annotation = annotation
}
return view
}
struct Constants {
static let AnnotationViewReuseIdentifier = "map cell"
}
}
My model is simply the var location:CLLocation?
我的模型只是
var location:CLLocation?
, and I update this value from my segue. ,然后从segue中更新此值。
I'm sure I get the correct coordinate in the println(). 我确定我在println()中得到了正确的坐标。
But Xcode always says 但是Xcode总是说
fatal error: unexpectedly found nil while unwrapping an Optional value
致命错误:解开Optional值时意外发现nil
And I found the nil
seems to be oldValue=(CLLocation?)nil
! 我发现
nil
似乎是oldValue=(CLLocation?)nil
!
Following is my simple class MapPin
which implements MKAnnotation
以下是我的简单类
MapPin
,它实现了MKAnnotation
class MapPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
I just had the exact same problem and fixed it! 我只是遇到了完全相同的问题并解决了! You're trying to add an annotation to a
mapView
that is not initialized yet. 您正在尝试向尚未初始化的
mapView
添加注释。
The problem is that you're setting the location
in prepareForSegue:
. 问题是您要在
prepareForSegue:
设置location
。 At this point your mapView
is nil. 此时,您的
mapView
为nil。 Call mapView.addAnnotation:
in viewDidLoad:
to fix it. 在
viewDidLoad:
调用mapView.addAnnotation:
进行修复。
I guess that's why it says "Do any additional setup after loading the view." 我猜这就是为什么它说“加载视图后进行任何其他设置”的原因。 because then all your outlets are initialized.
因为这样您的所有网点都将被初始化。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.