我正在使用位置显示功能
当运行我的应用程序时,它显示了这个消息->
Fatal error: 当隐式解包一个可选值时,意外地发现了nil:文件。
这是我的代码
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var mymap: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad() {
checkLocationSevices()
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func setupLocationManger()
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkLocationSevices()
{
if CLLocationManager.locationServicesEnabled()
{
setupLocationManger()
checkLocationAuthorizatiob()
}
else
{
}
}
func checkLocationAuthorizatiob()
{
switch CLLocationManager.authorizationStatus()
{
case .authorizedWhenInUse:
mymap.showsUserLocation = true
break
case .denied:
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .restricted:
break
case .authorizedAlways:
break
default:
break
}
}
}
这里是我的......这里错过了放->。
locationManager = CLLocationManager()
因为它只是像->.这样做。 var locationManager: CLLocationManager!
所以locationManager的值将是will,但当添加-> locationManager = CLLocationManager()
但当添加->时,你将会得到locationManager的值
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var mymap: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad() {
locationManager = CLLocationManager()
checkLocationSevices()
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func setupLocationManger()
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkLocationSevices()
{
if CLLocationManager.locationServicesEnabled()
{
setupLocationManger()
checkLocationAuthorizatiob()
}
else
{
}
}
func checkLocationAuthorizatiob()
{
switch CLLocationManager.authorizationStatus()
{
case .authorizedWhenInUse:
mymap.showsUserLocation = true
break
case .denied:
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .restricted:
break
case .authorizedAlways:
break
default:
break
}
}
}