fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

情到浓时终转凉″ 提交于 2019-12-13 08:47:39

问题


I'm trying to add marker in google maps with long touch in swift !!!!! and when i add below code some errors raised !

class ViewController: UIViewController, CLLocationManagerDelegate, UIGestureRecognizerDelegate {

    var mapView: GMSMapView?
    var locationManager = CLLocationManager()
    var locationMarker: GMSMarker!

    override func viewDidLoad() {
        super.viewDidLoad()

        let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:)))
        self.mapView!.addGestureRecognizer(longPressRecognizer)

        GMSServices.provideAPIKey("AIzaSyBw95wEhcSiSBmPWuYkiK0_IBnZQK-Lm7I")


        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error" + error.description)
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation = locations.last
        let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

        let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
                                                          longitude: userLocation!.coordinate.longitude, zoom: 16)
        let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
        view = mapView

 // marker with optional position
       let  position = CLLocationCoordinate2DMake(10, 10)
        let marker = GMSMarker(position: position)


        marker.opacity = 0.6
        marker.position = center
        marker.title = "Current Location"
        marker.snippet = ""
        marker.map = mapView
        //mapView.clear()
        //locationManager.stopUpdatingLocation()

        }
    func handleLongPress(recognizer: UILongPressGestureRecognizer)
    {
        if (recognizer.state == UIGestureRecognizerState.Began)
        {
            let longPressPoint = recognizer.locationInView(self.mapView);
            let coordinate = mapView!.projection.coordinateForPoint(longPressPoint )
            //Now you have Coordinate of map add marker on that location

        }}


    }

You can see error page here !!!!


回答1:


The problem is that you use map view without it is being initialized

var mapView: GMSMapView?

in viewDidLoad

    self.mapView!.addGestureRecognizer(longPressRecognizer)



回答2:


Error is raised in didUpdateLocations methods because its seems that sometimes we are not getting location so its passing nil on there.

So you can used try catch block or either use If let syntax to avoid this problem.



来源:https://stackoverflow.com/questions/37762422/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value-lldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!