Swift错误“线程1:EXC_BAD_INSTRUCTION”和致命错误:在解包可选值时意外发现nil

时间:2016-06-24 01:53:40

标签: ios swift debugging

错误“EXC_BAD_INSTRUCTION”发生并且出现胎儿错误“致命错误:在解开可选值时意外地发现nil”。 这是什么意思,我该如何解决?

import UIKit
import MapKit
import CoreLocation

class SliderViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var travelRadius: UILabel!
    @IBOutlet weak var mapKitView: MKMapView!
    @IBOutlet weak var slider: UISlider!
    private var lat : Double!
    private var long : Double!
    private var randomLatitude : Double!
    private var randomLongtitude : Double!
    let number1 : Double = 0.01449275362319 //Latitude 1 mile change
    let number2 : Double = 0.01810190649279 //Longtitude 1 mile change
    let random = Double(arc4random())/Double(UInt32.max) + 0


    var locationManager : CLLocationManager!

    var location : CLLocation!{
        didSet {
            lat = location.coordinate.latitude
            long = location.coordinate.longitude
        }
    }



    override func viewDidLoad() {
        sliderSlides(self)
  

错误:EXC_BAD_INSTRUCTION

        locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        checkCoreLocationPermission()

        self.mapKitView.showsUserLocation = true
    }



    @IBAction func sliderSlides(sender: AnyObject) {
        let miles = Double(self.slider.value)
        let delta = miles / 69.0
        var currentRegion = self.mapKitView.region
        currentRegion.span = MKCoordinateSpan(latitudeDelta: delta, longitudeDelta: delta)
        self.mapKitView.region = currentRegion
        travelRadius.text = "\(Int(round(miles))) mi"
        randomLatitude = lat + number1*(round(miles))*random
  

错误:EXC_BAD_INSTRUCTION               randomLongtitude = long + number2 *(round(miles))*(1-random)

    }





    func checkCoreLocationPermission() {
        if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
            locationManager.startUpdatingLocation() //Contains location details
        } else if CLLocationManager.authorizationStatus() == .NotDetermined {
            locationManager.requestWhenInUseAuthorization()
        } else if CLLocationManager.authorizationStatus() == .Restricted {
            print("Unauthorized to use location.")
        }
    }


    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


        location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1.5,
            longitudeDelta: 1.5))


        self.mapKitView.setRegion(region, animated: false)

        self.locationManager.stopUpdatingLocation()
    }
    func  locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print ("Errors:" + error.localizedDescription)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "sendLocationData") {

            let nav = segue.destinationViewController as! UINavigationController
            let addEventViewController = nav.topViewController as! DestinationViewController
            addEventViewController.latitude = randomLatitude
            addEventViewController.longitude = randomLongtitude

        }

}
}

Some Logs might be helpful

1 个答案:

答案 0 :(得分:0)

消息“在展开可选值时意外地发现nil”意味着,你强行展开一个nil的可选项。

在这种情况下,您将lat定义为: private var lat : Double!其中!向编译器指示变量应自动解包。此变量在此处使用之前未设置:randomLatitude = lat + number1*(round(miles))*random,导致崩溃。

在设置location之前加载视图,或者location可能是nil。看你的截图:

your screen shot