Swift 地图:在隐式展开可选值时意外发现 nil
Swift Maps: Unexpectedly found nil while implicitly unwrapping an Optional value
我正在使用位置显示功能
当 运行 我的应用程序显示此消息时->
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: 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
}
}
}
我也加了ti info.plist ->
这是我得到的错误图像
这里有问题 ->
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
}
}
}
我正在使用位置显示功能
当 运行 我的应用程序显示此消息时->
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: 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
}
}
}
我也加了ti info.plist ->
这里有问题 ->
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
}
}
}