简体   繁体   English

mapView.addAnnotation()说:“在展开可选值时意外找到nil”

[英]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说我的oldValuenil (在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.

相关问题 展开Optional值时意外发现nil - unexpectedly found nil while unwrapping an Optional value 展开可选的value()时意外找到nil - Unexpectedly found nil while unwrapping an Optional value() Swift:“ mapView.showUserLocation = true”返回“严重错误:在展开可选值(lldb)时意外发现nil” - Swift: “mapView.showUserLocation = true” returns “fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) ” 值不是nil,但是在展开Optional值时意外发现nil - value is not nil but getting unexpectedly found nil while unwrapping an Optional value 它在tableview的cellforrowatindexpath中崩溃,表示:“致命错误:在展开可选值时意外发现nil” - It crashes in cellforrowatindexpath of tableview , says : “fatal error: unexpectedly found nil while unwrapping an Optional value” 在iOS中,继续收到“在解包可选值时意外发现nil”的致命错误 - Keep getting a fatal error that says “unexpectedly found nil while unwrapping an Optional value” in iOS 打开UIViewController时解开Optional值时意外发现nil - Unexpectedly found nil while unwrapping an Optional value while opening UIViewController 迅捷的Xcode:解开可选值(lldb)时意外发现nil - swift Xcode: unexpectedly found nil while unwrapping an Optional value (lldb) ARQuicklook-致命错误:展开一个可选值时意外发现nil - ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value 标头自定义视图:在展开可选值时意外发现nil - Header Custom View: unexpectedly found nil while unwrapping an Optional value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM