我正在使用swift4.2并且Xcode 10我正在尝试使iOS应用程序使用位置服务,但它给了我一个例外:Fatal error: Unexpectedly found nil while unwrapping an Optional value
所以我试图检查位置是否返回 nil 所以我复制我的代码并打印位置并返回null,我Xcode从产品>方案>编辑方案>默认位置和调试区域中的检查位置模拟位置,并模拟到位置我选择任何一个知道问题所在吗?
import CoreLocation
class LocationVC: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentlocation:CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
authorizelocationstates()
}
func authorizelocationstates(){
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentlocation = locationManager.location
print(currentlocation)
} …Run Code Online (Sandbox Code Playgroud)