myLocationEnabled - 展开时意外发现 nil
myLocationEnabled - Unexpectedly found nil while unwrapping
我只是想使用 GoogleMaps API 在我的 MapViewController 中获取用户位置。
我到处环顾四周,无法弄清楚为什么我不断收到致命错误:在展开包装时意外发现 nil 并在此特定位置提供可选值。
这是我的代码:
class MapViewController: UIViewController {
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera = GMSCameraPosition.cameraWithLatitude(-34.9290, longitude:138.6010, zoom:10)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
mapView.mapType = kGMSTypeNormal
print("Calling addMarker")
// addMarker()
self.view = mapView
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
这是我收到错误的地方:
我知道 mapView.myLocationEnabled
返回 nil 但不明白 why/how。
很可能是 mapView 为 nil,而不是 mapView.myLocationEnabled。当您遇到崩溃时,请尝试以下 lldb 命令:
po mapView
那会告诉你确切的。
didChangeAuthorizationStatus 委托可能正在后台调用或在视图完成之前调用。您应该将这些扩展程序移动到您的 AppDelegate 中。
我只是想使用 GoogleMaps API 在我的 MapViewController 中获取用户位置。 我到处环顾四周,无法弄清楚为什么我不断收到致命错误:在展开包装时意外发现 nil 并在此特定位置提供可选值。
这是我的代码:
class MapViewController: UIViewController {
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera = GMSCameraPosition.cameraWithLatitude(-34.9290, longitude:138.6010, zoom:10)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
mapView.mapType = kGMSTypeNormal
print("Calling addMarker")
// addMarker()
self.view = mapView
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
这是我收到错误的地方:
我知道 mapView.myLocationEnabled
返回 nil 但不明白 why/how。
很可能是 mapView 为 nil,而不是 mapView.myLocationEnabled。当您遇到崩溃时,请尝试以下 lldb 命令:
po mapView
那会告诉你确切的。
didChangeAuthorizationStatus 委托可能正在后台调用或在视图完成之前调用。您应该将这些扩展程序移动到您的 AppDelegate 中。