我正在使用位置显示功能
当运行我的应用程序时,它向我显示此消息->
启用金属API验证致命错误:意外地发现nil,同时隐式展开一个可选值:file
这是我的代码
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的值将是,但是当添加-> 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
}
}
}