错误“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
}
}
}