swift3--致命错误:在展开可选值时意外发现nil

swift3--致命错误:在展开可选值时意外发现nil,swift,Swift,调用此代码行时,Swift 3中出现致命错误: let dummyAnnotations = getDummyAnnotations(centerLatitude: lat, centerLongitude: lon, delta: delta, count: count) 似乎无法将中心纬度和中心经度的类型正确转换为双精度 import UIKit import MapKit import CoreLocation class MapViewController: UIViewContro

调用此代码行时,Swift 3中出现致命错误:

let dummyAnnotations = getDummyAnnotations(centerLatitude: lat, centerLongitude: lon, delta: delta, count: count)
似乎无法将
中心纬度
中心经度
的类型正确转换为
双精度

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var userLocation: CLLocation?

override func viewDidLoad() {
super.viewDidLoad()

mapView.userTrackingMode = MKUserTrackingMode.followWithHeading

if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}

}

}

extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
self.userLocation = userLocation.location

let lat = self.userLocation!.coordinate.latitude
let lon = self.userLocation!.coordinate.longitude
let delta = 0.05
let count = 10

let dummyAnnotations = getDummyAnnotations(centerLatitude: lat, centerLongitude: lon, delta: delta, count: count)
}
}

fileprivate func getDummyAnnotations(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int) -> Array
{
var annotations: [ARAnnotation] = []

srand48(3)
for i in stride(from: 0, to: count, by: 1)
{
let annotation = ARAnnotation()
annotation.location = getRandomLocation(centerLatitude: centerLatitude, centerLongitude: centerLongitude, delta: delta)
annotation.title = "POI \(i)"
annotations.append(annotation)
}
return annotations
}

fileprivate func getRandomLocation(centerLatitude: Double, centerLongitude: Double, delta: Double) -> CLLocation
{
var lat = centerLatitude
var lon = centerLongitude

let latDelta = -(delta / 2) + drand48() * delta
let lonDelta = -(delta / 2) + drand48() * delta
lat = lat + latDelta
lon = lon + lonDelta
return CLLocation(latitude: lat, longitude: lon)
}
致命错误的详细信息如下:

fatal error: unexpectedly found nil while unwrapping an Optional value

libswiftCore.dylib`function signature specialization of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never:
0x100611184 : stp x26, x25, [sp, #-80]!
0x100611188 : stp x24, x23, [sp, #16]
0x10061118c : stp x22, x21, [sp, #32]
0x100611190 : stp x20, x19, [sp, #48]
0x100611194 : stp x29, x30, [sp, #64]
0x100611198 : add x29, sp, #64 ; =64
0x10061119c : mov x19, x6
0x1006111a0 : mov x20, x5
0x1006111a4 : mov x21, x4
0x1006111a8 : mov x22, x3
0x1006111ac : mov x23, x2
0x1006111b0 : mov x24, x1
0x1006111b4 : mov x25, x0
0x1006111b8 : adr x8, #936936 ; protocol descriptor for Swift.CVarArg + 88
0x1006111bc : nop
0x1006111c0 : add x0, x8, #16 ; =16
0x1006111c4 : movz w1, #0x28
0x1006111c8 : orr w2, wzr, #0x7
0x1006111cc : bl 0x1006114b0 ; rt_swift_allocObject
0x1006111d0 : mov x8, x0
0x1006111d4 : stp x22, x21, [x8, #16]
0x1006111d8 : strb w20, [x8, #32]
0x1006111dc : str w19, [x8, #36]
0x1006111e0 : adr x3, #241008 ; partial apply forwarder for Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never).(closure #2)
0x1006111e4 : nop
0x1006111e8 : mov x0, x25
0x1006111ec : mov x1, x24
0x1006111f0 : mov x2, x23
0x1006111f4 : mov x4, x8
0x1006111f8 : bl 0x100504b80 ; function signature specialization ) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer) -> ()]> of generic specialization of Swift.StaticString.withUTF8Buffer ((Swift.UnsafeBufferPointer) -> A) -> A
-> 0x1006111fc : brk #0x1

不要强制展开选项,而是先检查它是否有值。。。见下文:

    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    self.userLocation = userLocation.location

if(self.userLocation != nil) { // this check will ensure you only execute code when self.userLocation has a value - this way force unwrapping it will be safe

    let lat = self.userLocation!.coordinate.latitude
    let lon = self.userLocation!.coordinate.longitude
    let delta = 0.05
    let count = 10

    let dummyAnnotations = getDummyAnnotations(centerLatitude: lat, centerLongitude: lon, delta: delta, count: count)
}

    }

不要在任何地方使用强制展开!它的意思是要小心使用,并且只有在您知道它不是可选的,但无法向编译器说明的情况下才使用。谢谢您的及时回复!似乎centerLatitude、centerLatitude的类型无法正确转换为Double。但是如何修改代码?@user1188849这不是问题所在。问题是您正在使用
,强制展开选项而不检查零。您需要修复此问题。您确定错误发生在
getDummyAnnotations
行上吗?我估计它实际上发生在
self.userLocation!。坐标纬度
,在这种情况下,
self.userLocation
nil
。请通读。与其回答重复的问题,不如投票以重复的形式结束问题:注意。非常感谢。