我正在使用swift4.2
和Xcode 10
,并且我试图使iOS
应用使用位置服务,但它给了我例外:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
所以我尝试检查location是否返回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)
}
else{
locationManager.requestWhenInUseAuthorization()
authorizelocationstates()
}
}